android - 如何以 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());
}
解决方案
推荐阅读
- kubernetes - Kubernetes tls/ssl 使用 .pfx 证书
- d3.js - 如何在 dc.js 中设置 yScale
- java - Wildfly 19.1.0 的 Maven Servlet 依赖项
- amazon-athena - 空格分隔文件的 Grok 自定义模式
- android - 如何在我的颤振应用程序中使汉堡图标更大
- asp.net - linq 工作问题中的 iEnumrable 与 IQueryable
- sql - Oracle SQL Developer 显示过去 90 天的数据
- azure - Azure Policy - 使用逻辑应用设置警报
- python - 将 ctypes 与 DLL 一起使用 - 接收不正确的返回值
- arrays - Mongoose 一次性保存动态子文档数组