如何在Android中使用FCM获取多个通知作为whatsapp这样的组

时间:2016-11-14 12:30:31

标签: android google-cloud-messaging android-notifications firebase-cloud-messaging


我已经尝试下面的代码行来获取多个通知lilke whatsapp但是没有得到解决方案请检查我的代码并让我知道我在哪里做错了?
我正在使用fcm实现这一目标,请帮我解决这个问题

Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent,
                PendingIntent.FLAG_ONE_SHOT);

        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle("Firebase Push Notification")
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent)
                .setNumber(4);


            numMessages=numMessages+1;

            for (int i = 0; i <= numMessages; i++) {
                notificationBuilder.setContentText(messageBody)
                        .setNumber(numMessages);
                NotificationManager notificationManager =
                        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

                notificationManager.notify(0, notificationBuilder.build());

            }

请查看下面的图片,其中我已使用FCM显示了我想要实施哪种类型的群组消息 enter image description here

2 个答案:

答案 0 :(得分:0)

你错过了setGroup()。此功能是将所有通知添加到单个组(分组通知)。

有关详细信息,请参阅我的回答here

答案 1 :(得分:0)

您可能正在谈论InboxStyle次通知。你可以这样做:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
    .setSmallIcon(R.drawable.notification_icon)
    .setContentTitle("Event tracker")
    .setContentText("Events received")
NotificationCompat.InboxStyle inboxStyle =
        new NotificationCompat.InboxStyle();
String[] events = new String[6];
// Sets a title for the Inbox in expanded layout
inboxStyle.setBigContentTitle("Event tracker details:");
...
// Moves events into the expanded layout
for (int i=0; i < events.length; i++) {

    inboxStyle.addLine(events[i]);
}
// Moves the expanded layout object into the notification object.
mBuilder.setStyle(inBoxStyle);
...
// Issue the notification here.
mNotificationManager.notify(mId, mBuilder.build());

请参阅此处了解详情:InboxStyle Notifications