Can FCM notification on Android overwrite previous

2020-05-27 08:33发布

I'm using FCM to send notifications to Android devices. When the app is in background, if I send 10 notifications, the devices will show up 10 entries on the notification bar.

I want FCM to make only one entry on notification bar, i.e. the newer one will overwrite old ones. I don't find a key to set this at https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream.

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

2条回答
唯我独甜
2楼-- · 2020-05-27 08:54

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

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

Cheers.

查看更多
爷、活的狠高调
3楼-- · 2020-05-27 09:12

It's possible. Two approaches.

First is you make use of the collapse_key parameter to set the message as a collapsible message. Referring to the FCM docs:

A collapsible message is a message that may be replaced by a new message containing the same collapse key if it has yet to be delivered to the device.

It's actually included in the link you provided (first parameter under Options):

collapse_key - This parameter identifies a group of messages (e.g., with collapse_key: "Updates Available") that can be collapsed, so that only the last message gets sent when delivery can be resumed. This is intended to avoid sending too many of the same messages when the device comes back online or becomes active.

Note that there is no guarantee of the order in which messages get sent.

Note: A maximum of 4 different collapse keys is allowed at any given time. This means a FCM connection server can simultaneously store 4 different send-to-sync messages per client app. If you exceed this number, there is no guarantee which 4 collapse keys the FCM connection server will keep.


Second approach is Notification bundling/stacking/grouping. As per my answer here:

By grouping the notification, I'm presuming you mean stacking or bundling notifications.

This is more on how you handle the notification in your client app. You simply have to make use of the setGroup() to add all your notifications to a single group then calling notify() to let the NotificationManager of the changes.

This Add Each Notification to a Group documentation pretty much sums it all up.


Update:

From one of the linked posts, using the tag parameter is also an option:

Identifier used to replace existing notifications in the notification drawer.

If not specified, each request creates a new notification.

If specified and a notification with the same tag is already being shown, the new notification replaces the existing one in the notification drawer.

查看更多
登录 后发表回答