How to merge multiple notifications on tray into one?
See original GitHub issueI’m sending push notifications with axios from one device to another through fcm server. I can receive the notification. As I’m working on a chat app, each time I press _onSendMessage, i’ll do a post request to fcm server. Each time I’ll be sending multiple individual notifications and ended up flooding my notification tray with 4-8 separate notifications from the same app.
How do I merge all of them together?
I looked up and seems like we need to use collapse_key
. Here’s my code for sending the pushes, which I included the collapse_key but it isn’t merging.
const config = { headers: { 'Content-Type': 'application/json', 'Authorization': 'key=myAPIKey' } };
const content = JSON.stringify({ to: deviceToken, collapse_key: "replace", data: { 'title' : 'You have new messages!', 'color' : 'red', 'message' : 'Someone has sent you a message!', }; });
Axios.post('https://fcm.googleapis.com/fcm/send', content, config);
Is it because collapse_key is not supported in this version or i’m writing something wrong? Thanks in advance!
Issue Analytics
- State:
- Created 6 years ago
- Reactions:6
- Comments:9
Top GitHub Comments
if anyone is still looking for a solution or alternatives, try react-native-one-signal which also supports grouping.
Just send a POST request that looks something like this:
const config = { headers: { 'Content-Type': 'application/json', 'Authorization': 'Basic <Your Key>' } }; const body = JSON.stringify({ "app_id": <your app id>, "include_player_ids": <users u want to notify>, "contents": {"en": "Hello"}, "android_group": "stack", "priority": 10 }); Axios.post('https://onesignal.com/api/v1/notifications', body, config);
It will automatically combine notifications with the same android_group, therefore becoming one single notification that displays the content of all other notifications of its android_group( like WhatsApp’s 1 to 1 chat notification when you receive more than two lines of messages.)
As for collapsing all notifications of the same android_group into one single notification that shows the latest notification, I have yet to find a solution.
I’ve experimented with silent notifications. The idea is to intercept the silent notification in the app, then create a local notification in an attempt to collapse them.
I’ve tried calling
PushNotification.cancelLocalNotifications({id})
as per the documentation, but it doesn’t remove the notification from the notification center.Separately the documentation at https://github.com/zo0r/react-native-push-notification#2-cancelalllocalnotifications says
NOTE: there is currently no api for removing specific notification alerts from the notification centre.
It’s not clear whether that statement is referring to iOS, Android or both.
I think this needs to be a change on the native side. iOS APN has the field
apns-collapse-id
https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/sending_notification_requests_to_apns