Android 从Firebase中删除过期通知

Android 从Firebase中删除过期通知,android,firebase,firebase-cloud-messaging,firebase-notifications,Android,Firebase,Firebase Cloud Messaging,Firebase Notifications,我的应用程序使用Firebase通知。如果Firebase生成两个具有相同id的通知,我会在状态栏中同时显示这两个通知,但我希望删除较旧的通知 我的代码: private void sendNotification(String messageBody) { Intent intent = new Intent(this, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.F

我的应用程序使用Firebase通知。如果Firebase生成两个具有相同
id
的通知,我会在状态栏中同时显示这两个通知,但我希望删除较旧的通知

我的代码:

  private void sendNotification(String messageBody) {
    Intent intent = new Intent(this, MainActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
            PendingIntent.FLAG_ONE_SHOT);

    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("FCM Message")
            .setContentText(messageBody)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent);
    NotificationManager notificationManager =
            (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(119);

    notificationManager.notify(119 /* ID of notification */, notificationBuilder.build());
}

如果您知道要删除的通知Id,只需使用

    notificationManager.cancel(id);
如果你不这样做,那么使用

        notificationManager.cancelAll();

如果您知道要删除的通知Id,只需使用

    notificationManager.cancel(id);
如果你不这样做,那么使用

        notificationManager.cancelAll();

您希望发送一条所谓的可折叠消息,这意味着任何具有相同密钥的新消息都会用该密钥替换以前的消息。查看是否对每个通知使用相同的id。以前的通知应替换为新通知。请如何添加关键参数?如果您自己使用问题中的代码生成通知,则可以使用相同的ID再次调用notify,这将更新现有通知或创建新通知(如果是第一次)。请注意,如果您从Firebase控制台发送通知,在某些情况下,它会在不使用代码的情况下生成通知。您希望发送一条所谓的可折叠消息,这意味着任何具有相同密钥的新消息都会用该密钥替换以前的消息。查看是否对每个通知使用相同的id。以前的通知应替换为新通知。请如何添加关键参数?如果您自己使用问题中的代码生成通知,则可以使用相同的ID再次调用notify,这将更新现有通知或创建新通知(如果是第一次)。请注意,如果您从Firebase控制台发送通知,在某些情况下,它会在不使用代码的情况下生成通知。Firebase将哪个id设置为通知?Firebase将哪个id设置为通知?