android - 组 fcm 通知像 whatsapp 但允许多个组通知

标签 android firebase push-notification android-notifications 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());

通过添加 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参数。它会根据需要对通知进行分组,

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

一些有用的链接:

FCM Documentation

Similar SO question

关于android - 组 fcm 通知像 whatsapp 但允许多个组通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43631963/

相关文章:

java - 检测apk下载来源

android - 以编程方式更改 EditText 的下划线颜色

reactjs - 如何使用uid从firebase获取数据

ios - 如何在 Firebase 中将项目更新/添加到 JSON 数组?

iphone - iOS 中的 Appcelerator 云推送通知

iphone - Apple 向特定用户推送通知

android - 确定 Android 权限级别

android - 通过远程 xml 设置布局

firebase - 未处理的异常 : Invalid argument(s) (onError): The error handler of Future. catchError 必须返回 future 类型的值

push-notification - 谷歌日历 api 推送通知 : 401 - Unauthorized WebHook callback channel