Android FCM notification grouping

2019-01-12 08:51发布

问题:

I was trying to implement FCM notification for a chat app and I want to achieve "WhatsApp-like" notification where the notification will be grouped by conversation.

Say when Alice replied to a conversation with Bob inside, Bob will receive a notification with the body "Alice: the content". But if Alice quickly reply to the conversation again, Bob will get another new notification but I just want to append the new reply onto the prev notification instead.

How can I achieve that?

回答1:

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.



回答2:

You can use the 'tag' in the notification payload:

tag Optional, string
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.

source: https://firebase.google.com/docs/cloud-messaging/xmpp-server-ref#notification-payload-support