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 通知时,将取消相应的本地通知。请注意,您需要根据自己的应用程序逻辑和需求进行适当的调整。

相关内容

热门资讯

一分钟了解!德州透视竞技联盟,... 自定义hhpoker有没有作弊挂系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅...
透视安卓版!pokemmo辅助... 透视安卓版!pokemmo辅助官网(透视)果然是真的有挂(详细辅助安装教程)进入游戏-大厅左侧-新手...
让我来分享经验!wepoker... 让我来分享经验!wepoker透视苹果系统,约局吧开挂神器是真的吗,大神讲解(有挂软件)准备好在约局...
透视脚本!wepoker私人局... 透视脚本!wepoker私人局开挂视频(透视)一贯有挂(详细辅助大神讲解);运辅助工具,进入游戏界面...
透视能赢!aapoker透视脚... 透视能赢!aapoker透视脚本入口(透视)其实真的是有挂(详细辅助辅助教程);暗藏猫腻,小编详细说...
揭秘一下!wepoker怎么获... 揭秘一下!wepoker怎么获得好牌,pokemomo辅助工具,细节方法(有挂攻略);人气非常高,a...
透视攻略!wepoker好友助... 透视攻略!wepoker好友助力码(透视)其实是真的有挂(详细辅助玩家教程)1)辅助挂:进一步探索辅...
总算了解!约局吧怎么看有没有挂... 总算了解!约局吧怎么看有没有挂,wepoker轻量版透视,解密教程(有挂攻略);玩家必备必赢加哟《1...
一分钟带你了解!哈糖大菠萝破解... 《一分钟带你了解!哈糖大菠萝破解器,we-poker辅助软件教程,专业教程(有挂教程)》 we-po...
透视代打!wpk俱乐部辅助器(... 透视代打!wpk俱乐部辅助器(透视)本来有挂(详细辅助新2025教程)1、完成透视辅助安装,帮助玩家...