组fcm通知,如whatsapp,但允许多个组通知 [英] Group fcm notifications like whatsapp but allowing multiple group notifications

查看:126
本文介绍了组fcm通知,如whatsapp,但允许多个组通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

假设我有一个类似Android应用程序的图库.当用户喜欢或评论图库中的照片时,我们将使用下面提供的代码触发fcm通知

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());

通过添加InboxStyle,我们可以将通知分组为一个,并增加计数.(例如,您有5条通知)

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);

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

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.

如果有帮助,我将获得照片的唯一ID.

I'll be receiving unique ids for the photos,if that helps.

How long will the id be retained?

让我们假设用户在ID为001的照片上发表了2条评论,并且合作伙伴以群组的形式收到了该通知.

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?

因为一组ID为001的通知保持不变.

Because a group of notification with id 001 remains untouched.

推荐答案

我会使用TAG参数.对于每组消息,您应该使用不同的TAG.

I would use the TAG parameter. For each group of messages you should use a different 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条通知.

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.

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

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.

一些有用的链接:

FCM文档

类似的SO问题

这篇关于组fcm通知,如whatsapp,但允许多个组通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆