通知没有堆叠?

时间:2017-12-19 17:17:31

标签: android android-notifications

您好我正在使用Firebase云消息传递服务进行通知。我能够显示通知,但它们没有堆叠。尽管有任何数量的通知,它们都会在通知托盘上单独显示。我正在使用相同的组ID进行个别通知和摘要通知。有人可以帮忙吗?

这是我的代码......

public class MyFirebaseMessagingService extends FirebaseMessagingService {
    final String STORY_GROUP = "story";
    String groupId;
    NotificationManagerCompat manager;

    public MyFirebaseMessagingService() {
    }

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);
        if (remoteMessage.getData().size() > 0) {

            Intent intent;
            Map<String, String> data = remoteMessage.getData();
            String notificationFor = data.get("notificationFor");
            String key, ownerId, picUrl, title, body,sendersUid, receiversUid, profilePicUrl, conversationId;

            if (notificationFor.equals("storyComment")) {
                key = data.get("key");
                ownerId = data.get("ownerId");
                picUrl = data.get("icon");
                title = data.get("title");
                body = data.get("body");
                groupId = STORY_GROUP;

                intent = new Intent(this, StoryDetails.class);
                intent.putExtra("key", key);
                intent.putExtra("ownerId", ownerId);

                sendNotification(intent,title,body,groupId,"New comments on your story");

           }

            Log.d("MessagingService", "Message received and has data payload :" + remoteMessage.getData());

        }

    }

    public void sendNotification(Intent intent,String title,String message,String groupId,String summaryText) {

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

        Log.d("NotificationService","groupId = " + groupId);

        PendingIntent pIntent = PendingIntent.getActivity(this,(int) System.currentTimeMillis(),intent,0);
        Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                                         .setShowWhen(true)
                                         .setContentTitle(title)
                                         .setContentText(message)
                                         .setSmallIcon(R.drawable.launcher)
                                         .setContentIntent(pIntent)
                                         .setGroup(groupId)
                                         .setAutoCancel(true)
                                         .setSound(notificationSound);

        NotificationCompat.Builder group = new NotificationCompat.Builder(this)
                                         .setGroupSummary(true)
                                         .setGroup(groupId);

        Random rand = new Random();
        int randomInt = rand.nextInt(100);
        if (manager == null) {
            manager = NotificationManagerCompat.from(this);
        }
        manager.notify(randomInt,builder.build());
        manager.notify(1,group.build());
   }
}

0 个答案:

没有答案