0

我的应用程序使用 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());
}
4

1 回答 1

2

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

    notificationManager.cancel(id);

如果你不那么使用

        notificationManager.cancelAll();
于 2016-07-30T12:44:07.933 回答