How to collapse multiple notifications into a single one using Firebase?
我在 Firebase Cloud Functions 中有一个函数,用于向我的应用程序中的特定用户发送通知,并且具有作为
1 2 3 4 5 6 7 8 | const notificationContent = { notification: { title:"My Notification Title", body:"My Notification Body", icon:"default", sound :"default" } }; |
我尝试使用
我可以通过 Firebase 实现这一目标吗?如果我收到多于一个通知,要归为一个?
提前致谢!
如果您想使用更多可定制和高级的通知功能。
您应该只发送带有
请记住,如果您使用
如果您发送带有
我希望这个链接会有所帮助。
我也有同样的困惑,并意识到我误解了 collapseKey 和 tag 的用途。
collapseKey 将限制客户端离线时收到的通知数量,标签是将通知堆叠在抽屉中的东西。
所以对于一个典型的云函数,它应该是这样的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | const notification = { notification: { 'title': 'Interesting title', 'body': 'Hello, world' }, 'data': { 'whatever': whatever, }, 'android':{ 'collapseKey': collapseKey, 'priority': 'high', 'notification': { 'tag': tag, } }, 'token': fcmToken }; admin.messaging().send(notification) |
请注意,"tag" 参数位于 android 通知内,而不是顶级通知内。
最简单、最灵活的解决方案是扩展
这是您的
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public class MyFCMService extends FirebaseMessagingService { @Override public void onMessageReceived(RemoteMessage remoteMessage) { if (remoteMessage.getNotification() != null) { //this notification was sent from the Firebase console, because in our cloud function, we are using the DATA tag, not the notification tag //so here we have to handle the notification that was sent from the console ... } else if (remoteMessage.getData().get(KEY) != null) { //this data message was sent from our cloud function //KEY is one of the keys that you are using on the cloud function //in your example, you are using the keys: title, body, icon, sound //display the notification to the user ... notificationManager.notify(TAG, ID, notificationBuilder.build()); //you have to use the same TAG and the same ID on each notification if you want your 2nd notification to simply update the text of the first one, instead of showing as a new notification } } } |
附注。当你从你的云函数发送通知时(如果你使用