Capacitor应用Android端读取FCM通知数据并清除指定通知方案
解决方案
针对你在Capacitor开发中遇到的FCM通知清除问题,这里提供两种可行的实现思路:
方案一:利用FCM通知标签(Tag)关联聊天ID
核心思路是将chatId作为FCM通知的tag字段,这样系统创建的通知会直接绑定该标签,后续可通过标签精准清除对应聊天的通知。
步骤1:修改FCM推送Payload
在android字段中添加notification.tag,将chatId作为标签值:
{ "android": { "priority": "high", "notification": { "tag": "631e162f5f286c23d13e06d5" // 直接传入目标chatId } }, "notification": { "title": "you have a new message from XYZ", "body": "hello" }, "data": { "messageId": "62971793-0111-4c9b-bbb2-bb78262a0543", "chatId": "631e162f5f286c23d13e06d5", "type": "CHAT_NEW_MESSAGE" }, "token": "fcmToken" }
步骤2:扩展Capacitor推送插件添加清除方法
在Android端的PushNotifications插件中新增清除指定标签通知的方法:
@PluginMethod() public void clearNotificationsByTag(PluginCall call) { String tag = call.getString("tag"); if (tag == null) { call.reject("Tag is required"); return; } NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE); // FCM默认使用0作为通知ID,直接通过标签+ID清除 notificationManager.cancel(tag, 0); call.resolve(); }
步骤3:前端调用清除逻辑
当用户进入目标聊天页面时,调用扩展方法清除对应标签的通知:
import { PushNotifications } from '@capacitor/push-notifications'; // 进入聊天页面时执行 const clearChatNotifications = async (chatId: string) => { await PushNotifications.clearNotificationsByTag({ tag: chatId }); };
优点:无需自定义通知创建逻辑,实现简单;缺点:同一标签的通知会被覆盖(同一聊天的多条新消息只会保留最新一条通知)。
方案二:自定义通知创建+本地映射存储
如果需要保留同一聊天的多条通知并全部清除,可通过纯data消息自定义创建通知,同时在本地存储中记录通知ID与chatId的映射关系。
步骤1:修改FCM推送Payload
移除notification字段,所有内容放在data中:
{ "android": { "priority": "high" }, "data": { "messageId": "62971793-0111-4c9b-bbb2-bb78262a0543", "chatId": "631e162f5f286c23d13e06d5", "type": "CHAT_NEW_MESSAGE", "title": "you have a new message from XYZ", "body": "hello" }, "token": "fcmToken" }
步骤2:修改插件的消息接收逻辑
在onMessageReceived中自定义创建通知,并保存通知ID与chatId的映射:
@Override public void onMessageReceived(RemoteMessage remoteMessage) { super.onMessageReceived(remoteMessage); Map<String, String> data = remoteMessage.getData(); String chatId = data.get("chatId"); String title = data.get("title"); String body = data.get("body"); // 创建通知渠道(Android O及以上必填) createNotificationChannel(); // 生成唯一通知ID int notificationId = (int) System.currentTimeMillis(); NotificationCompat.Builder builder = new NotificationCompat.Builder(getContext(), "chat_channel") .setSmallIcon(R.drawable.ic_notification) .setContentTitle(title) .setContentText(body) .setPriority(NotificationCompat.PRIORITY_HIGH); NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(notificationId, builder.build()); // 保存通知ID与chatId的映射到SharedPreferences SharedPreferences prefs = getContext().getSharedPreferences("notification_mappings", Context.MODE_PRIVATE); Set<String> ids = prefs.getStringSet(chatId, new HashSet<>()); ids.add(String.valueOf(notificationId)); prefs.edit().putStringSet(chatId, ids).apply(); } private void createNotificationChannel() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel channel = new NotificationChannel( "chat_channel", "Chat Notifications", NotificationManager.IMPORTANCE_HIGH ); channel.setDescription("New chat messages"); NotificationManager notificationManager = getContext().getSystemService(NotificationManager.class); notificationManager.createNotificationChannel(channel); } }
步骤3:添加批量清除聊天通知的方法
@PluginMethod() public void clearChatNotifications(PluginCall call) { String chatId = call.getString("chatId"); if (chatId == null) { call.reject("chatId is required"); return; } SharedPreferences prefs = getContext().getSharedPreferences("notification_mappings", Context.MODE_PRIVATE); Set<String> notificationIds = prefs.getStringSet(chatId, new HashSet<>()); NotificationManager notificationManager = (NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE); // 批量清除对应通知 for (String idStr : notificationIds) { int id = Integer.parseInt(idStr); notificationManager.cancel(id); } // 清除本地映射记录 prefs.edit().remove(chatId).apply(); call.resolve(); }
步骤4:前端调用清除逻辑
import { PushNotifications } from '@capacitor/push-notifications'; // 进入聊天页面时执行 const clearChatNotifications = async (chatId: string) => { await PushNotifications.clearChatNotifications({ chatId }); };
优点:支持保留同一聊天的多条通知,清除时可全部移除;缺点:需要自定义通知创建逻辑,代码量稍大。
内容的提问来源于stack exchange,提问作者mahendra
相关产品推荐
相关产品推荐

