Android通知 - 相同标签的本地和FCM通知不会合并
创始人
2024-08-21 18:30:07
0

在 Android 中,如果您发送具有相同标签的本地通知和 FCM(Firebase Cloud Messaging)通知,它们不会自动合并。但是,您可以使用以下方法手动实现此功能:

  1. 首先,确保您的应用程序中已经启用了 FCM 并正确配置了通知通道。此外,确保您的应用程序可以接收和处理 FCM 推送通知。

  2. 在发送 FCM 通知时,确保您设置了相同的 tag 值。例如:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id")
        .setSmallIcon(R.drawable.ic_notification)
        .setContentTitle("FCM Notification")
        .setContentText("This is an FCM notification")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setTag("notification_tag");

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, builder.build());
  1. 在本地通知中,使用相同的 tag 值来设置通知。例如:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "channel_id")
        .setSmallIcon(R.drawable.ic_notification)
        .setContentTitle("Local Notification")
        .setContentText("This is a local notification")
        .setPriority(NotificationCompat.PRIORITY_DEFAULT)
        .setTag("notification_tag");

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(0, builder.build());
  1. 接下来,在您的应用程序的 onMessageReceived() 方法中,处理 FCM 通知,并检查是否存在具有相同 tag 值的本地通知。如果存在,则取消该本地通知。例如:
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
    if (remoteMessage.getNotification() != null) {
        String tag = remoteMessage.getNotification().getTag();

        if (tag != null) {
            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
            notificationManager.cancel(tag, 0);
        }
    }
}

通过以上步骤,您可以确保具有相同标签的本地通知和 FCM 通知不会合并,并且当收到 FCM 通知时,将取消相应的本地通知。请注意,您需要根据自己的应用程序逻辑和需求进行适当的调整。

相关内容

热门资讯

第九分钟辅助!微信小程序蜀山四... 第九分钟辅助!微信小程序蜀山四川血战辅助,胡易决胜麻架辅助(透视实锤开挂辅助平台)>>您好:软件加薇...
盘点透视"WePOK... 盘点透视"WePOKer辅助软件挂"开挂(透视)辅助工具(分享给玩家技巧教程)盘点透视"WePOKe...
3分钟透视!微乐小程序多功能修... 3分钟透视!微乐小程序多功能修改器,epoker免费透视脚本(透视讲解开挂辅助神器) >>您好:软件...
正版透视"微扑克挂脚... 正版透视"微扑克挂脚本"开挂(透视)辅助挂(盘点一款必胜教程)正版透视"微扑克挂脚本"开挂(透视)辅...
第七分钟透视!微信小程序青龙大... 第七分钟透视!微信小程序青龙大厅辅助,新蛮王辅助器(透视app开挂辅助工具) 了解更多开挂安装加(1...
正品透视"wepok... 正品透视"wepoker辅助器介绍"开挂(透视)辅助插件(总算了解解密教程)正品透视"wepoker...
十分钟开挂!朋友局辅助器,we... 十分钟开挂!朋友局辅助器,wepoker数据分析工具(实测揭晓开挂辅助脚本);1、点击下载安装,朋友...
实测透视"wpk辅助... 实测透视"wpk辅助会封号最简单三个办法"开挂(透视)辅助平台(盘点一款规律教程)实测透视"wpk辅...
第六分钟透视!微信小程序中至赣... 第六分钟透视!微信小程序中至赣牌圈辅助,德州真人透视脚本(玩家必备科普开挂辅助平台);无需打开直接搜...
必看透视"wepok... 必看透视"wepoker透视挂的优势"开挂(透视)辅助挂(一分钟揭秘教你教程)必看透视"wepoke...