React-native-push-notification: How to merge multiple notifications on tray into one?

Created on 28 Mar 2017  ·  9Comments  ·  Source: zo0r/react-native-push-notification

I'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!

Most helpful comment

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.

All 9 comments

Any news on this?
Have you tried to update to this new mysterious v3?

I've been looking for a solution to this issue but I'm confused, mostly because you say that collapse_key is not supported but in the troubleshooting doc the field is present in two examples.

@fufuninja and @kelset the parameter "collapse_key", only works when the device is offline and when comes back online again it will only show the last notification of a given "collapse_key".

Hey @nickmendes thanks for the clarification... so there is no way to merge the push notifications at all?

@kelset I've never tried with this specific package, Android you can use the following:

    tag: 'some_tag', // (optional) add tag to message
    group: "group", // (optional) add group to message

iOS im not sure about :/

iOS apparently never groups in any case (!) but thanks for the suggestion regarding Android. Do I need to put these fields into the metadata of the push or in the body?

Outside notification object. How are you sending your notifications atm ?

We use Parse, so I think I'll be able to add those fields - I'll keep you updated ✌🏻

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

Was this page helpful?
0 / 5 - 0 ratings