Android FCM - 如何只显示一个通知 [英] Android FCM - how to show only one notification

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

问题描述

我正在从 FCM 向 Android 设备发送推送通知,这是通过向 FCM 发送包含 JSON 正文的 POST 消息来完成的.

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.

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

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.

collapse_key"应该可以解决这个问题,对吧?(FCM 文档)

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

但是应该在哪里或如何插入呢?

But where or how should it be inserted?

这个 SO 问题回答了这个问题,但没有给出示例:Can FCMAndroid 上的通知会覆盖之前的通知吗?

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

当前 JSON 正文:

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--"
    ]
}

我已经尝试了多种方法来包含collapse_key",但到目前为止还没有运气.仍然是多个通知.欢迎任何帮助.

I have tried in many ways to include the "collapse_key" but no luck so far. Still multiple notifications. Any help is welcome.

推荐答案

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

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.

因此,使用此 JSON 只会显示一个通知:

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!

如果有人想进一步解释collapse_key",我会很高兴,显然我误解了.

And if someone wishes to explain further on "collapse_key" I would be glad, clearly I misunderstood it.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆