2

我不确定这是否完全可行,但我会陈述我的要求。如果有可能,那么请好好帮我弄清楚如何去做。fcm通知类似于whatsapp,但允许多个组通知

比方说,我有一个类似的Android应用程序。当用户喜欢上或在图库中的照片的评论,我们会使用以下

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我们可以组notifcations到一个单一的一个,只是增加了计数,触发FCM通知。(例如你有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); 

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

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

How long will the id be retained? 

假设用户滴在照片2条评论id为001和合作伙伴收到通知为一组。

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

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

+0

您可以使用['tag'](https://firebase.google.com/docs/cloud-messaging/http-server -ref#notification-payload-support)参数并传递照片的唯一标识。 –

+0

使用'photo unique id'作为通知ID,它应该没问题。他们将按通知ID进行分组。 – Shark

+0

ID会保留多久? 让我们假设用户在ID为001的照片上放下2条评论,并且合作伙伴收到作为一个组的通知。 当用户在ID为002的照片上放下另外2条评论时会发生什么? 会有2组吗? 由于ID为001的一组通知保持不变。 –

回答

0

我会使用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

+1

是的,我们可以使用标签或使用唯一的ID作为参数为 - 'notificationManager.notify(notifyid,notificationBuilder.build());' –

+1

问题是** Message1 - ** _将有一个正文陈述 - 用户喜欢photo123._ ** Message3 ** - _将有一个正文 - 用户对此照片发表了评论123_ **由此产生的群组应该拥有您在photo123 **上有“2”通知的内容,而** Message2 - ** _ will保留为通知与身体 - 用户喜欢photoABC_ –

+0

我明白了。该逻辑属于您的应用程序。如果喜欢的数量大于1,则必须能够计数“喜欢”,然后发送消息“您在photo123上有#个通知”。如果喜欢的数量是1,只需发送“用户在photo123上评论”。只需发送正确的消息,就是这样。 –