MayaB
MayaB

Reputation: 189

Add Action To Firebase Notification

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!!

Upvotes: 8

Views: 11672

Answers (4)

MayaB
MayaB

Reputation: 189

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.

Upvotes: 3

gdenuf
gdenuf

Reputation: 890

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.

Upvotes: 0

Diego Giorgini
Diego Giorgini

Reputation: 12717

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.

Upvotes: 8

Alexander N.
Alexander N.

Reputation: 1592

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.

Upvotes: 0

Related Questions