类似WhatsApp的分组fcm通知,但允许多个组通知。

5

我不确定是否完全可能,但我会说明我的需求。如果可能的话,请帮助我找出如何实现它。

假设我有一个画廊类的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的照片上留下了两条评论,而合作伙伴会以组的形式收到通知。

What happens when the user drops another 2 comments on photo with id 002?
Will there be 2 groups?

由于带有 id 001 的一组通知未被触及,因此出现了这种情况。


你可以使用tag参数并传递照片的唯一标识符。 - AL.
请使用“照片唯一标识符”作为通知ID,并确保它们按通知ID分组。 - Shark
ID会被保留多久? 假设用户在ID为001的照片上留下了2条评论,合作伙伴以组的形式收到通知。 当用户在ID为002的照片上再次留下2条评论时会发生什么? 会有2个组吗? 因为ID为001的通知组仍然存在。 - Shyamnath Mallinathan
2
我刚刚检查了,我没有得到分离的组。同一组往往会被覆盖。 - Shyamnath Mallinathan
1个回答

2
我会使用标签参数。对于每组消息,您应该使用不同的标签。
例如:
消息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参数。它可以按您需要对通知进行分组。
希望我表述清楚并能帮到您。
一些有用的链接: FCM文档 类似的SO问题

1
是的,我们可以使用标签或将唯一ID作为参数来使用 notificationManager.notify(notifyid, notificationBuilder.build()) - Shyamnath Mallinathan
1
问题在于消息1-将有一个内容声明-用户喜欢照片123。 消息3-_将有一个内容-用户评论了这张照片123_。结果组应该有内容,你有“2”条关于照片123的通知,而消息2-_将保持为通知,内容为-用户喜欢照片ABC_。 - Shyamnath Mallinathan
我明白了。这个逻辑属于你的应用程序。你必须能够计算“赞”的数量,如果赞的数量大于1,则发送消息“您在photo123上有#条通知”。如果赞的数量为1,则只需发送“用户评论了photo123”。只需发送正确的消息即可。 - Federico Alvarez
在 FCM 文档中,我找不到关于“标签”字段的任何文档。有什么建议吗?我需要在应用程序中实现消息分组。 - NKM
NKM,你有没有按照答案中提供的“FMC文档”链接进行操作?它在页面的中间左右位置描述。或者只需在该页面上查找单词TAG。 - Federico Alvarez

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接