How to collapse multiple notifications into one using Firebase?

I have a function in the Firebase cloud functions that is used to send notifications to specific users in my application and has the following code as notificationContent :

 const notificationContent = { notification: { title: "My Notification Title", body: "My Notification Body", icon: "default", sound : "default" } }; 

I tried using collapse_key: "unique_key" , but that does not affect. I read that this only works when the device is turned off. I also used tag: "unique" , but every time a new notification arrives, it cancels the oldest.

Is there a way I can achieve this with Firebase? If I receive more than one notification, which should be grouped one by one?

enter image description here

Thanks in advance!

+1
source share
3 answers

If you want to use more customizable and advanced notification features. You should send FCM only with the data payload and create notifications on the android client side.
Remember that if you send FCM with a notification or notification + data payload, the notification will be generated by the Android kernel system, and the BroadcastReceiver onReceive method will not be called if your application is in the background.
If you send FCM with the data payload, it will call onReceive all the time, so you can create a custom notification manually on the android client side. (Most applications use the latter method.)

I hope this link is helpful.

+1
source

The simplest and most flexible solution is to extend the FirebaseMessagingService and handle the notification yourself. But first, instead of using notification on your notificationContent in your cloud function, you should change this to data so that you send a data message instead of a notification message. The difference is that the notification message will have an implicit collapse key (application package name), while the data message will not have it. But the data message must be processed on the client, otherwise it will not be displayed.

Here is an example of what you will need for a FirebaseMessagingService :

 public class MyFCMService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) { if (remoteMessage.getNotification() != null) { //this notification was sent from the Firebase console, because in our cloud function, we are using the DATA tag, not the notification tag //so here we have to handle the notification that was sent from the console ... } else if (remoteMessage.getData().get(KEY) != null) { //this data message was sent from our cloud function //KEY is one of the keys that you are using on the cloud function //in your example, you are using the keys: title, body, icon, sound //display the notification to the user ... notificationManager.notify(TAG, ID, notificationBuilder.build()); //you have to use the same TAG and the same ID on each notification if you want your 2nd notification to simply update the text of the first one, instead of showing as a new notification } } } 

PS. When you send a notification from your cloud function (well, if you use the data tag, this is actually a data message, not a notification message), then this method will be called, regardless of whether the application is in the background or in the foreground. HOWEVER , when you send a notification from the firebase console, this method will be called ONLY if the application is in the foreground. If the application is in the background, the Firebase SDK will process the notification and show it to the user. In some cases, it makes sense to show a notification only when the user does not start the application, for example, if you want to advertise some new features of the application. In this case, you can use a unique tag in the notification console (for example, "display_in_foreground") and check this tag on the client. If you set it to true, you can show the notification even to users who are currently running the application, or if it is false, you can skip the notification. This check will only be performed if the application is in the foreground. If it is in the background, it will not be called at all, and the SDK will process to show a notification.

+1
source

I got the same confusion, and I realized that I misunderstood what collapseKey and tag are for.

collapseKey will limit the number of notifications that the client receives while it is not online, the tag is what will add notifications to the mailbox.

So for a typical cloud function, it should look like this:

  const notification = { notification: { 'title': 'Interesting title', 'body': 'Hello, world' }, 'data': { 'whatever': whatever, }, 'android':{ 'collapseKey': collapseKey, 'priority': 'high', 'notification': { 'tag': tag, } }, 'token': fcmToken }; admin.messaging().send(notification) 

Please note that the tag parameter is located inside the Android notification, not the top-level notification.

0
source

Source: https://habr.com/ru/post/1257484/


All Articles