Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can FCM notification on Android overwrite previous one?

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.

like image 760
Romulus Urakagi Ts'ai Avatar asked Sep 30 '16 01:09

Romulus Urakagi Ts'ai


People also ask

How does FCM work on Android?

The FCM backend receives the message request, generates a message ID and other metadata, and sends it to the platform specific transport layer. When the device is online, the message is sent via the platform-specific transport layer to the device. On the device, the client app receives the message or notification.

Does FCM store data?

In other words, FCM guarantees best effort for messages that must be delivered "now or never." Keep in mind that a time_to_live value of 0 means messages that can't be delivered immediately are discarded. However, because such messages are never stored, this provides the best latency for sending notification messages.

What does FCM name mean when I turn on my phone?

Firebase Cloud Messaging (FCM) is a cross-platform messaging solution that lets you reliably send messages at no cost. Firebase. 339K subscribers. Introducing Firebase Cloud Messaging.


2 Answers

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.

like image 35
AL. Avatar answered Oct 05 '22 22:10

AL.


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

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

Cheers.

like image 51
Ayo Makanjuola Avatar answered Oct 05 '22 22:10

Ayo Makanjuola