首页 > 解决方案 > 如何以 Whatsapp 样式堆叠或分组 Firebase 云消息传递通知

问题描述

我正在努力让我的 FCM 通知以与 Whatsapp 相同的方式堆叠:

1) 显示特定房间的最新消息,以及所有未读消息的更新计数。

2) 为不同房间的通知指示点 1 的单独分组。

我花了几个小时查看各种 SO 问题,是我找到答案的最接近的一个。问题是我使用的是数据有效负载,而不是通知,并且“标签”属性似乎不适用于我的数据有效负载。

当前行为是仅显示最新通知,覆盖以前的通知。

这是我的 sendNotification() 方法FirebaseMessagingService

private void sendNotification(RemoteMessage remoteMessage) {
        //Intent intent = new Intent(this, StudentChatActivity.class);
        String clickAction = remoteMessage.getData().get("click_action");
        Intent intent = new Intent(clickAction);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        String roomID = remoteMessage.getData().get("ROOMID");
        String origin = remoteMessage.getData().get("ORIGIN");
        String msgID = remoteMessage.getData().get("MSGID");
        Log.d(TAG, "Message data payload, roomID: " + roomID);


        intent.putExtra("ROOMID", roomID);
        intent.putExtra("USERID", UserID);
        intent.putExtra("USERNAME", UserName);
        intent.putExtra("ORIGIN", origin);

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

        String channelId = getString(R.string.received_message);
        Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, channelId)
                        .setSmallIcon(R.drawable.small_pickle)
                        .setContentTitle("FCM Message")
                        .setContentText(remoteMessage.getData().get("body"))
                        .setPriority(1)
                        .setAutoCancel(true)
                        .setSound(defaultSoundUri)
                        .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        // Since android Oreo notification channel is needed.
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId,
                    "Channel human readable title",
                    NotificationManager.IMPORTANCE_HIGH);
            notificationManager.createNotificationChannel(channel);
        }



        Log.d(TAG, "Noification ID: " + notify_no);
        notificationManager.notify(0 /* ID of notification */, notificationBuilder.build());
    }

标签: androidfirebasepush-notificationfirebase-cloud-messaging

解决方案


推荐阅读