问题
I am not sure whether this is entirely possible but I'll state my requirement. If it is possible then kindly help me figure out how to do it.
Let's say I have a gallery kind of an android app. When the user likes or comments on a photo in the gallery, We'd trigger an fcm notification using the code given below
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());
By adding InboxStyle we can group notifcations into a single one and just increase the count.(For e.g. You have 5 notifications)
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);
But my requirement is like separate grouping for separate photos. If the user leaves 2 comments each for 3 photos. I need three groups of notifications to be listed. More like you have 2 comments on this photo,2 on this and so on.
I'll be receiving unique ids for the photos,if that helps.
How long will the id be retained?
Let's assume the user drops 2 comments on photo with id 001 and the partner receives the notification as a group .
What happens when the user drops another 2 comments on photo with id 002?
Will there be 2 groups?
Because a group of notification with id 001 remains untouched.
回答1:
I would use the TAG parameter. For each group of messages you should use a different TAG.
For example:
Message 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--"
]
}
Message 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--"
]
}
Message 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--"
]
}
This will show only 2 notification alerts. One for Photo123, showing there are 2 notifications (last message), and the other for PhotoABC, showing there is just 1 notification.
The most important thing here is the TAG parameter. It will group notifications as you need,
Hope I made myself clear at it helps you out.
Some helpful links:
FCM Documentation
Similar SO question
来源:https://stackoverflow.com/questions/43631963/group-fcm-notifications-like-whatsapp-but-allowing-multiple-group-notifications