Android FCM - how to show only one notification

I send push notifications from FCM to the Android device, this is done by sending a POST message to the FCM containing the JSON body.

If I send the same JSON body twice, the Android device will display two notifications (or three, or four, ...). But I want him to show only one.

"collapse_key" should solve this problem, right? ( FCM Documentation )

But where and how should it be inserted?

This SO question answers this, but no example is given: Can the FCM notification on Android overwrite the previous one?

Current JSON body:

{ "notification": { "title": "MyAPP", "body": "Open MyAPP to access your data", "click_action" : "OPEN_MAINACTIVITY", "icon": "ic_launcher_red", "color": "#ff0000" }, "data": { "extra1":"sample1", "extra2":"sample2" }, "registration_ids":[ "--my_id--" ] } 

I tried in many ways to enable "collapse_key", but so far no luck. A few more notifications. Any help is appreciated.

+1
source share
1 answer

Well, I kept digging and found the answer: it was not a "collapse_key", I had to use the "tag" in the notification instead.

So, using this JSON, only one notification is displayed:

 { "notification": { "title": "MyAPP", "body": "Open MyAPP to access your data", "click_action" : "OPEN_MAINACTIVITY", "icon": "ic_launcher_red", "color": "#ff0000" "tag": "unique_tag" }, "data": { "extra1":"sample1", "extra2":"sample2" }, "registration_ids":[ "--my_id--" ] } 

Hope this helps others!

And if someone wants to explain further "collapse_key", I would be glad, it is clear that I misunderstood it.

+3
source

Source: https://habr.com/ru/post/1257489/


All Articles