如何显示只有一个通知

如何显示只有一个通知

本文介绍了Android的FCM - 如何显示只有一个通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从FCM向Android设备发送推送通知,这是通过向包含JSON主体的FCM发送一条POST消息来完成的。



如果发送相同的JSON身体两次,Android设备将显示两个通知(或三,或四,...)。但我希望它只显示一个。



collapse_key应该解决这个问题,对不对? ()



但是在哪里或如何插入?

这个问题回答了这个问题,但没有给出例子:



当前JSON正文:

  {
notification:{
title:MyAPP,
body:打开MyAPP访问您的数据,
click_action:OPEN_MAINACTIVITY,
icon:
color:#ff0000
},
data:{
extra1:sample1,
extra2: sample2
},
registration_ids:[
--my_id--

}

我在很多方面都尝试过t o包括collapse_key,但目前还没有运气。仍然是多个通知。任何帮助都是值得欢迎的。

解决方案

好吧,我一直在挖,找到答案:不是collapse_key,我是应该在通知中使用tag选项。



所以通过使用这个JSON,只显示一个通知:

  {
通知:{
title:MyAPP,
body:打开MyAPP来访问您的数据,
click_action:OPEN_MAINACTIVITY,
icon:ic_launcher_red,
color:#ff0000
tag:unique_tag
},
data:{
extra1:sample1,
extra2:sample2
},
registration_ids :[
--my_id--

}



$ b $
$ b

b

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

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

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

But where or how should it be inserted?

This SO question answers this but no example is given: Can FCM notification on Android overwrite 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 have tried in many ways to include the "collapse_key" but no luck so far. Still multiple notifications. Any help is welcome.

解决方案

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

So by using this JSON only one notification is shown:

{
    "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 wishes to explain further on "collapse_key" I would be glad, clearly I misunderstood it.

这篇关于Android的FCM - 如何显示只有一个通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 14:31