简体   繁体   中英

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

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM