Android: 如何检测notification id是否存在?

Android: How to detect whether notification id exist or not?

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

mNotificationManager.notify(100, mBuilder.build());

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

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

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

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