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

相关内容

热门资讯

透视ai代打!wepoke辅助... 透视ai代打!wepoke辅助德之星,wepoke透明挂辅助软件是真的吗,详细透视解密教程小薇(透视...
we辅助poker德之星!aa... we辅助poker德之星!aa扑克网上的挂真的(透视)好像存在有挂(详细辅助揭秘教程)是一款可以让一...
透视神器!WePoKe外 挂,... 透视神器!WePoKe外 挂,wopoker用ai有用吗,详细透视科技教程;1、上手简单,内置详细流...
wepoke有没有挂!德扑之星... wepoke有没有挂!德扑之星怎么设置(透视)起初真的是有挂(详细辅助新2025教程)是一款可以让一...
透视教程!wepoke辅助挂,... 透视教程!wepoke辅助挂,wepoke靠谱吗,详细透视透牌教程1、wepoke辅助挂系统规律教程...
aapoker透明挂!扑克时间... aapoker透明挂!扑克时间辅助(透视)一向是有挂(详细辅助可靠教程);是一款可以让一直输的玩家,...
透视计算!wepoke辅助有挂... 透视计算!wepoke辅助有挂,wepoke苹果版外挂,详细透视科技教程1、首先打开wepoke辅助...
wepoke ai辅助!德扑a... wepoke ai辅助!德扑ai助手软件(透视)原生存在有挂(详细辅助2025新版)1、不需要AI权...
透视中牌率!wepoke辅助,... 透视中牌率!wepoke辅助,we辅助poker,详细透视必备教程1、wepoke辅助机器人多个强度...
wpk发牌逻辑!微扑克ai代打... wpk发牌逻辑!微扑克ai代打(透视)好像有挂(详细辅助技巧教程),您好,wpk发牌逻辑这款游戏可以...