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

相关内容

热门资讯

黑科技代打!(德州之星)外挂辅... 黑科技代打!(德州之星)外挂辅助挂,(德州之星)固有是有挂,透明挂教程(有挂透视)是一款可以让一直输...
德扑之星发牌规律!wepowe... 德扑之星发牌规律!wepower系统规律,wepoke模拟器,透明教程(先前是有挂);科技安装教程;...
微扑克德州专用辅助器!来玩德州... 微扑克德州专用辅助器!来玩德州app苹果下载,智星德州菠萝有人机吗,解密教程(其实是真的有挂);支持...
黑科技透明挂!(wePoke)... 黑科技透明挂!(wePoke)软件透明挂,(WepOke)好像有挂,揭秘教程(有挂方法);支持多人共...
黑科技透明挂!(wpK)透视辅... 黑科技透明挂!(wpK)透视辅助挂,(WPk)原生真的有挂,存在挂教程(有挂软件);支持多人共享记分...
红龙扑克是不是有问题!wepo... 红龙扑克是不是有问题!wepoke软件透明下载渠道,德扑之星玩的是啥,曝光教程(原来是真的有挂);小...
黑科技脚本!(WepokE)软... 黑科技脚本!(WepokE)软件透明挂,(wePOke)总是真的是有挂,透明挂教程(有挂透视)准备好...
aapoker有内部好嘛!wo... 1、aapoker有内部好嘛!wopoker分析器,wpk透视辅助可测试真的,透明挂教程(都是是有挂...
黑科技私人局!(AAPOKer... 黑科技私人局!(AAPOKer)透明挂辅助安装,(aapOKER)从前存在有挂,技巧教程(有挂透视)...
微扑克ai辅助!wepower... 微扑克ai辅助!wepower软件靠谱吗,wpk外挂存在吗,AI教程(素来真的是有挂);小薇(透视辅...