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

相关内容

热门资讯

了解透视!wepoker怎么看... 了解透视!wepoker怎么看底牌,微乐辅助工具哪个好用,方式教程(有挂总结)-哔哩哔哩1、起透看视...
揭露透视!hhpoker有后台... 揭露透视!hhpoker有后台操作吗,微乐内蒙古自建房脚本免费下载,方案教程(有挂方式)-哔哩哔哩1...
详情透视!佛手在线大菠萝为什么... 详情透视!佛手在线大菠萝为什么都输,小程序微乐陕西挖坑辅助器,秘籍教程(讲解有挂)-哔哩哔哩1、进入...
总结透视!hhpoker辅助软... 总结透视!hhpoker辅助软件,微乐小程序辅助器代理,手筋教程(有挂方法)-哔哩哔哩1)hhpok...
分享透视!黑侠破解wepoke... 分享透视!黑侠破解wepoker,小程序微乐辅助器2.0免费,秘籍教程(有挂工具)-哔哩哔哩1、下载...
关于透视!约局吧开挂,微乐广西... 关于透视!约局吧开挂,微乐广西小程序脚本,课程教程(有挂技术)-哔哩哔哩1、操作简单,无需约局吧开挂...
专业透视!wepoker透视苹... 专业透视!wepoker透视苹果系统,微乐辅助靠谱麻,项目教程(有挂技巧)-哔哩哔哩1、进入游戏-大...
曝光透视!pokerworld... 曝光透视!pokerworld破解版下载,微乐游戏兑换码大全,课程教程(有挂助手)-哔哩哔哩1、po...
详细透视!hhpoker德州有... 详细透视!hhpoker德州有挂吗,微乐斗地主有挂吗,诀窍教程(证实有挂)-哔哩哔哩1、首先打开hh...
曝光透视!hhpoker辅助挂... 曝光透视!hhpoker辅助挂是真的吗,陕西微乐小程序脚本,积累教程(有挂分析)-哔哩哔哩1、下载好...