Android上的FCM通知可以覆盖上一个? [英] Can FCM notification on Android overwrite previous one?

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

问题描述

我正在使用FCM向Android设备发送通知。当应用程序在后台,如果我发送10个通知,设备将在通知栏上显示10个条目。



我希望FCM在通知中只有一个条目吧,即新的会覆盖旧的。我没有在 https:// firebase.google.com/docs/cloud-messaging/http-server-ref#downstream



有没有办法,或者是不可能的?谢谢。

解决方案

这是可能的。两种方法。

首先,您使用 collapse_key 参数将消息设置为可折叠消息。请参阅 FCM文档


可折叠消息是一条消息,如果消息尚未传送到设备,消息可能会被包含相同折叠键的新消息所取代。 p>

它实际包含在选项下选择第一个参数): $ b

collapse_key - 此参数标识可以折叠的一组消息(例如,使用collapse_key:Updates Available),以便只能在恢复传递时发送最后一条消息。这是为了避免在设备恢复在线或激活时发送太多相同的消息。



请注意,不能保证消息的顺序发送。

注意:在任何给定时间最多允许4个不同的折叠键。这意味着FCM连接服务器可以同时为每个客户端应用程序存储4个不同的发送到同步消息。如果超过这个数字,则不能保证FCM连接服务器将保留4个合拢键。







第二种方法是通知捆绑/堆叠/分组。按照我的答案这里


<通过分组通知,我假设您的意思是堆叠捆绑通知



这更关于如何处理客户端应用程序中的通知。你只需要使用 setGroup()将所有通知添加到一个组,然后调用 notify()让NotificationManager发生变化。

这个将每个通知添加到组几乎总结






更新:

链接的帖子之一,使用 tag 参数也是一个选项:


用于替换通知抽屉中现有通知的标识符。

如果未指定,则每个请求都会创建一个新通知。



如果已指定并且具有相同标记的通知已被显示,则新通知将替换通知抽屉中的现有通知。



I'm using FCM to send notifications to Android devices. When the app is in background, if I send 10 notifications, the devices will show up 10 entries on the notification bar.

I want FCM to make only one entry on notification bar, i.e. the newer one will overwrite old ones. I don't find a key to set this at https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream.

Is there a way to do, or it is impossible? Thanks.

解决方案

It's possible. Two approaches.

First is you make use of the collapse_key parameter to set the message as a collapsible message. Referring to the FCM docs:

A collapsible message is a message that may be replaced by a new message containing the same collapse key if it has yet to be delivered to the device.

It's actually included in the link you provided (first parameter under Options):

collapse_key - This parameter identifies a group of messages (e.g., with collapse_key: "Updates Available") that can be collapsed, so that only the last message gets sent when delivery can be resumed. This is intended to avoid sending too many of the same messages when the device comes back online or becomes active.

Note that there is no guarantee of the order in which messages get sent.

Note: A maximum of 4 different collapse keys is allowed at any given time. This means a FCM connection server can simultaneously store 4 different send-to-sync messages per client app. If you exceed this number, there is no guarantee which 4 collapse keys the FCM connection server will keep.


Second approach is Notification bundling/stacking/grouping. As per my answer here:

By grouping the notification, I'm presuming you mean stacking or bundling notifications.

This is more on how you handle the notification in your client app. You simply have to make use of the setGroup() to add all your notifications to a single group then calling notify() to let the NotificationManager of the changes.

This Add Each Notification to a Group documentation pretty much sums it all up.


Update:

From one of the linked posts, using the tag parameter is also an option:

Identifier used to replace existing notifications in the notification drawer.

If not specified, each request creates a new notification.

If specified and a notification with the same tag is already being shown, the new notification replaces the existing one in the notification drawer.

这篇关于Android上的FCM通知可以覆盖上一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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