像 Whatsapp 一样对 fcm 通知进行分组,但允许多个组通知

Shy*_*han 5 android push-notification android-notifications firebase firebase-cloud-messaging

我不确定这是否完全可能,但我会说明我的要求。如果可能的话请帮我弄清楚该怎么做。

假设我有一个类似画廊的 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());
Run Code Online (Sandbox Code Playgroud)

通过添加 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);
Run Code Online (Sandbox Code Playgroud)

但我的要求就像对单独的照片进行单独分组。如果用户为 3 张照片各留下 2 条评论。我需要列出三组通知。更像是您对这张照片有 2 条评论,对这张照片有 2 条评论,等等。

如果有帮助的话,我将收到照片的唯一 ID。

How long will the id be retained?
Run Code Online (Sandbox Code Playgroud)

假设用户对 ID 为 001 的照片发表了 2 条评论,并且合作伙伴作为一个群组收到了通知。

What happens when the user drops another 2 comments on photo with id 002?
Will there be 2 groups?
Run Code Online (Sandbox Code Playgroud)

因为一组 id 为 001 的通知保持不变。

Fed*_*rez 2

我会使用 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--"
    ]
}
Run Code Online (Sandbox Code Playgroud)

留言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--"
    ]
}
Run Code Online (Sandbox Code Playgroud)

留言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--"
    ]
}
Run Code Online (Sandbox Code Playgroud)

这将仅显示 2 个通知警报。一个用于 Photo123,显示有 2 个通知(最后一条消息),另一个用于 PhotoABC,显示只有 1 个通知。

这里最重要的是TAG参数。它将根据您的需要对通知进行分组,

希望我说得清楚对你有帮助。

一些有用的链接:

FCM 文档

类似的问题