FCM:我想将所有通知分组为一个。我怎样才能做到这一点?

时间:2017-02-16 11:30:13

标签: android firebase

我使用FCM接收通知。当我收到多个通知时,它会填满通知状态栏。如何将它们组合成一个?

我的代码:

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("ABC")
                .setPriority(Notification.PRIORITY_HIGH)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setGroup(GROUP_KEY_NOTIFICATIONS)
                .setContentIntent(pendingIntent);

使用setGroup()它不起作用。

编辑:

  

.setGroupSummary(真)
  适用于OS棒棒糖,但不适用于棉花糖。你能帮忙吗

1 个答案:

答案 0 :(得分:-1)

要对通知进行分组,您必须在数据库中插入通知,并使用NotificationCompat.InboxStyle对通知进行分组。

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("ABC")
                .setPriority(Notification.PRIORITY_HIGH)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setGroup(GROUP_KEY_NOTIFICATIONS)
                .setContentIntent(pendingIntent);

     NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

                    for (int i = 0; i < data.size(); i++)
                    {

                        inboxStyle.addLine(data.get(i));
                    }

                    inboxStyle.addLine("");

                notificationBuilder.setStyle(inboxStyle);

从包含所有通知的数据库中提取数据的列表。