【问题标题】:Android: How to detect whether notification id exist or not?Android:如何检测通知ID是否存在?
【发布时间】:2016-04-03 18:44:38
【问题描述】:

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

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

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

【问题讨论】:

    标签: android push-notification broadcastreceiver android-notifications intentservice


    【解决方案1】:

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

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

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

    【讨论】:

    • 是的,这可行,但我无法更改为 API 级别 23。我能再问您一个问题吗?如何在从BroadcastReceiver 扩展的类中使用HttpURLConnection
    • 您应该为这个问题创建另一个问题。但简短的回答是启动服务。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-21
    • 1970-01-01
    • 2018-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多