7

I'm trying to add an action button to notifications produced on firebase 9.0.0, for Android, when app is in background.

Any ideas?

Thanks!!

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
MayaB
  • 179
  • 1
  • 1
  • 10

4 Answers4

8

firebase-cloud-messaging doesn't provide an API to add action buttons to the notifications.

You can request new features to the firebase library here: https://firebase.google.com/support/contact/bugs-features/

For now what you can do is to send a data-message with a custom payload via the server-side API You can then receive that payload in onMessageReceived() and generate your custom notification.

Diego Giorgini
  • 12,489
  • 1
  • 47
  • 50
  • Thank you! my issue was that I tried to send the notification from Firebase console. adding "data" key didn't work for me that way... – MayaB May 29 '16 at 13:07
3

So in order to customize notifications when app is on background, as Diego mentioned, the only current way is to create the notification yourself. Adding "data" key to notification payload, results in onMessageReceived() callback, on which you can create any notification andy notify.

Thing is, I tried to send a notification from Firebase console, rather then from the API. There I couldn't add the data key properly and catch it. From API all works fine.

Milad Faridnia
  • 9,113
  • 13
  • 65
  • 78
MayaB
  • 179
  • 1
  • 1
  • 10
0

I would consider reviewing the following documentation:

Handle messages in a backgrounded app

https://firebase.google.com/docs/cloud-messaging/downstream#backgrounded

This looks like you would need to change the intent filter for your FirebaseMessagingService to handle the OnClick action.

Alexander N.
  • 1,458
  • 14
  • 25
  • Thanks for the reply, Alexander! I'm trying to customize the notification itself, and add an action to it. However, click_action defines what should happen when clicking on the entire notification. – MayaB May 29 '16 at 09:56
0

Assuming you already know that you have to use only data key (I answered here, too) to send to your application in the background, you can add an action to notification message builder like this:

final NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.addAction(
                    R.drawable.ic_setting_light,
                    res.getString(**R.string.Your_Button_String**),
                    PendingIntent.getActivity(
                            context,
                            0,
                            **Your_Intent_To_Open_When_Button_Is_Click**,
                            PendingIntent.FLAG_UPDATE_CURRENT));

Of course this has to be inside handling logic from Android side.

Note: Notification actions are supported only in Android 4.1 or later.

Community
  • 1
  • 1
gdenuf
  • 840
  • 1
  • 12
  • 16