我不确定这是否完全可行,但我会说明我的要求。如果可能的话,请帮我弄清楚该怎么做。
假设我有一个类似画廊的 Android 应用程序。当用户喜欢或评论图库中的照片时,我们会使用下面给出的代码触发 fcm 通知
value++;
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification)
.setLargeIcon(rawBitmap)
.setContentTitle("MyApp")
.setContentText(ContentText)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setDefaults(Notification.DEFAULT_SOUND)
.setStyle(new NotificationCompat.BigTextStyle().bigText(ContentText))
.setContentIntent(pendingIntent);
NotificationManager notificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(value, notificationBuilder.build());
通过添加 InboxStyle,我们可以将通知分组为一个并增加计数。(例如,您有 5 个通知)
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
// Sets a title for the Inbox in expanded layout
inboxStyle.setBigContentTitle("Title - Notification");
inboxStyle.setSummaryText("You have "+value+" Notifications.");
notificationBuilder.setStyle(inboxStyle);
但我的要求就像对单独的照片进行单独分组。如果用户对 3 张照片各留下 2 条评论。我需要列出三组通知。更像是您对这张照片有 2 条评论,对这张照片有 2 条评论,依此类推。
如果有帮助,我会收到照片的唯一 ID。
How long will the id be retained?
假设用户对 ID 为 001 的照片发表了 2 条评论,并且合作伙伴作为一个群组收到了通知。
What happens when the user drops another 2 comments on photo with id 002?
Will there be 2 groups?
因为一组 id 为 001 的通知保持不变。
最佳答案
我会使用 TAG 参数。对于每组消息,您应该使用不同的 TAG。
例如:
留言1)
{
"notification": {
"title": "PhotoApp: photo 123",
"body": "You have 1 notification",
"click_action" : "OPEN_MAINACTIVITY",
"icon": "ic_launcher",
"color": "#ffffff"
"tag": "photo123"
},
"registration_ids":[
"--your_id--"
]
}
留言2)
{
"notification": {
"title": "PhotoApp: photo ABC",
"body": "You have 1 notification",
"click_action" : "OPEN_MAINACTIVITY",
"icon": "ic_launcher",
"color": "#ffffff"
"tag": "photoABC"
},
"registration_ids":[
"--your_id--"
]
}
消息3)
{
"notification": {
"title": "PhotoApp: photo 123",
"body": "You have 2 notifications",
"click_action" : "OPEN_MAINACTIVITY",
"icon": "ic_launcher",
"color": "#ffffff"
"tag": "photo123"
},
"registration_ids":[
"--your_id--"
]
}
这将仅显示 2 个通知警报。一个用于 Photo123,显示有 2 个通知(最后一条消息),另一个用于 PhotoABC,显示只有 1 个通知。
这里最重要的是TAG参数。它会根据需要对通知进行分组,
希望我说清楚这对你有帮助。
一些有用的链接:
关于android - 组 fcm 通知像 whatsapp 但允许多个组通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43631963/