Can an FCM notification on Android overwrite the previous one?

I use FCM to send notifications to Android devices. When the application is in the background, if I send 10 notifications, the devices will display 10 entries in the notification panel.

I want FCM to make only one entry in the notification panel, i.e. the new one will overwrite the old ones. I cannot find a key to install this on https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream .

Is there a way to do this, or is it impossible? Thanks.

+5
source share
2 answers

It is possible. Two approaches.

First, you use the collapse_key parameter to set the message as a discarded message. Referring to FCM Documents :

A discarded message is a message that can be replaced with a new message containing the same collapse key if it has not yet been delivered to the device.

It is really included in the link you specified (the first parameter in the Parameters section):

collapse_key . This parameter identifies a group of messages (for example, with collapse_key: "Available Updates") that can be minimized so that only the last message can be sent when sending. This is done so as not to send too many identical messages when the device returns to the network or becomes active.

Please note that there is no guarantee which order is shipped.

Note. At any given time, a maximum of 4 different camber keys are allowed. This means that the FCM connection server can simultaneously store 4 different send messages for synchronization to the client application. If you exceed this number, there is no guarantee that keys with 4 keys will be closed by the FCM connection server.


The second approach is merging / grouping notifications / grouping. According to my answer here :

By grouping a notification, I assume that you mean stacking or linking notifications .

This is more about how you handle a notification in your client application. You just need to use setGroup () to add all your notifications to one group, and then call notify () to allow the NotificationManager to change.

This Add all notices to group documentation sums up pretty much all of this.


Update:

From one of the related messages , using the tag parameter is also an option:

The identifier used to replace existing notifications in the notification box.

If not specified, each request creates a new notification.

If indicated and a notification with the same tag is already displayed, the new notification replaces the existing one in the notification box.

+2
source

To achieve this, use the tag key in your notification payload

 { "notification" : { "title" : "Notification Title", "body" : "Notification Body", "tag" : "your_unique_tag" } } 

Greetings.

+2
source

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


All Articles