Android:如何检测通知ID是否存在?

Mar*_*cus 1 android broadcastreceiver push-notification android-notifications intentservice

是否可以检测通知栏中是否存在附带唯一ID的通知?

mNotificationManager.notify(100, mBuilder.build());
Run Code Online (Sandbox Code Playgroud)

表单示例,我创建了ID为100的通知.下次当我再次使用该ID创建通知时,我不希望它更新.我用了setOnlyAlertOnce(true),声音消失了,但它仍然更新了通知并将其移至顶部.

Mic*_*ael 5

从API Level 23(Android M)开始,您可以获得活动通知列表并查找具有给定ID的通知.

StatusBarNotification[] notifications = 
    mNotificationManager.getActiveNotifications();
for (StatusBarNotification notification : notifications) {
  if (notification.getId() == 100) {
    // Do something.
  }
}
Run Code Online (Sandbox Code Playgroud)

在早期版本中,您需要通过设置deleteIntent创建通知时保留有关您创建的通知的信息并处理通知删除.