【问题标题】:How to check which notifications are active in status bar in Android Dev?如何在 Android Dev 的状态栏中检查哪些通知处于活动状态?
【发布时间】:2011-04-07 12:59:06
【问题描述】:

我做了一个应用,可以在安卓手机的下拉状态栏中设置通知。但是,我的代码中有一个错误(有时通知已设置,有时未设置)。如果通知对用户可见,我希望能够检查(在代码中)。 (即用户可以在状态栏中看到通知吗?)。

我该怎么做? (提前致谢)。

非常感谢示例代码。

【问题讨论】:

    标签: android


    【解决方案1】:

    我希望能够检查(在代码中)通知是否对用户可见。 (即用户可以看到 状态栏中的通知?)。

    我该怎么做?

    你不能,抱歉。更新:现在可以使用 Android 4.3+ http://developer.android.com/reference/android/service/notification/NotificationListenerService.html#getActiveNotifications()

    但是,您始终可以简单地 cancel() 它 - 取消不在屏幕上的 Notification 非常好。相反,您始终可以安全地再次调用notify() 来调用相同的Notification,如果Notification 已经出现在屏幕上也不会造成问题。

    编辑:

    如果您不想使用NotificationListenerService,则在 API 23 中添加了NotificationManager.getActiveNotifications()

    【讨论】:

    • @weakwire:仅当通知尚未出现在屏幕上时。
    • 关于再次调用 notify() 的非常有用的信息 - 谢谢!
    • 它会重新振动并发出声音。有什么办法吗?
    • 我无法获得通知的状态,这很糟糕。由我自己的应用发布,无需注册NotificationListenerService(这需要监听通知的权限)。
    • @MHSFisher:对不起,我不明白你的问题。我建议您提出一个单独的 Stack Overflow 问题,您可以在其中提供 minimal reproducible example 以及有关您的问题的更多详细信息。
    【解决方案2】:

    只是将所有内容放在一起。这就是它的工作原理

    要构建通知,

            Notification n = new Notification.Builder(MyService.this)
                    .setContentTitle("Notification Title")
                    .setContentText("Notification Message")
                    .setSmallIcon(R.drawable.myicon).build();
    

    要发出通知声音呼叫setSound() 的通知,

            Uri alarmSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
            Notification n = new Notification.Builder(MyService.this)
                    .setContentTitle("Notification Title")
                    .setContentText("Notification Message")
                    .setSound(alarmSound)
                    .setSmallIcon(R.drawable.myicon).build();
    

    要在用户选择并启动接收者Intent后取消通知,请致电setAutoCancel()

            Notification n = new Notification.Builder(MyService.this)
                    .setContentTitle("Notification Title")
                    .setContentText("Notification Message")
                    .setSound(alarmSound)
                    .setAutoCancel(true)
                    .setSmallIcon(R.drawable.myicon).build();
    

    要为特定通知只发出声音/振动一次,请使用Notification.FLAG_ONLY_ALERT_ONCE。使用此标志,您的通知只会发出一次声音,直到它被取消,您可以使用通知 ID 多次调用 notify()。请注意,如果您调用 cancel() 或者如果用户取消通知或自动取消,则 notify() 调用将使通知再次发出声音。

            n.flags |= Notification.FLAG_ONLY_ALERT_ONCE;    // Dont vibrate or make notification sound
    

    最后把通知放到通知面板上,

            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    
            notificationManager.notify(notification_id, n);
    

    请注意,如果您想有效地使用通知,这里的notification_id 很重要。(为通知保持单一声音/振动或取消特定通知)。

    要取消特定通知,

            NotificationManager notificationManager =
                    (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            notificationManager.cancel(notification_id);
    

    即使通知不存在,您也可以cancel() 发送通知,或者您可以使用相同的 ID 多次调用 notify()。请注意,使用不同的 id 调用 notify 将创建新的通知。

    因此,无论通知是否存在,如果您再次调用 notify() 并设置正确的 notification_id 并设置 Notification.FLAG_ONLY_ALERT_ONCE 标志,您可以保持通知处于活动状态,而不会因重复的声音而打扰用户。

    【讨论】:

    • .setOnlyAlertOnce(true) 用于 NotificationCompat
    【解决方案3】:

    您需要为您发出的每个通知设置一个 id。

    所以你发出通知..

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, notId + selectedPosition, intent, 0);
    
    AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, rightNow.getTimeInMillis() - offset, pendingIntent);
    
    Notification notification = new Notification(R.drawable.icon, "TVGuide Υπενθύμιση", System.currentTimeMillis());
    NotificationManager manger = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE);
    notification.setLatestEventInfo(context, "Κανάλι: " + b.getString("channel"), "Εκπομπή: " + showname, pendingIntent);
    manger.notify(notId, notification);
    

    清除它..

    PendingIntent pendingIntent = PendingIntent.getBroadcast(context,notId, intent, 0); 
    pendingIntent.cancel();
    

    并检查是否处于活动状态..(如果没有可用的待处理意图,existAlarm 返回 null)

     public PendingIntent existAlarm(int id) {
      Intent intent = new Intent(this, alarmreceiver.class);
      intent.setAction(Intent.ACTION_VIEW);
      PendingIntent test = PendingIntent.getBroadcast(this, id + selectedPosition, intent, PendingIntent.FLAG_NO_CREATE);
      return test;
     }
    

    所以一切都归结为为每个通知初始化一个 ID 以及如何使其唯一。

    【讨论】:

    • @CommonsWare:如果我调用带有待处理意图的通知,我可以检查是否存在待处理意图,然后通知也存在
    • 啊,我知道你要去哪里了。有趣的技术——我没有为此尝试过或在其他地方看到它。
    • 很长一段时间......但仍然......它适用于后 GB 版本吗?如果我使用 TaskStackBuilder 创建通知,我该如何使用这个 sn-p?
    • existAlarm 中的“选定位置”是什么?
    • 很好的解决方案。 @PuruPawar 是的,至少在 7.1
    【解决方案4】:

    API 23 中的 NotificationManager 类引入了一个新方法:

    public StatusBarNotification[] getActiveNotifications()
    

    【讨论】:

    • 我期待在 2019 年使用它。
    • 我写的是关于 android.app.NotificationManager::getActiveNotifications 而不是 NotificationListenerService。
    • 我们在 PRE - Marshmallow 版本 (API 23) 上有解决方案吗?
    • @ZaBlanc API 19 仍然拥有很大的市场份额。抱歉,您需要再等一下
    【解决方案5】:

    有一个标志。

    Notification notification = new Notification(icon, tickerText, when);
    notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
    

    FLAG_ONLY_ALERT_ONCE:

    ...如果您希望每次发送通知时都播放声音和/或振动,则应设置,即使在此之前尚未取消。

    虽然再次发送通知时闪烁,但不会有任何声音或振动。

    【讨论】:

      【解决方案6】:

      【讨论】:

        【解决方案7】:

        seems 从 Android M (API 23) 中获取您的进程是可能的,而无需使用 NotificationListenerService 也不需要额外的权限:

        notificationManager.getActiveNotifications()

        【讨论】:

          【解决方案8】:

          从 Android Marshmallow (API 23) 开始,您可以恢复应用发布的活动通知列表。这个 NotificationManager 方法是getActiveNotifications()。更多信息在这里:https://developer.android.com/reference/android/app/NotificationManager.html#getActiveNotifications()

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2011-10-07
            • 1970-01-01
            • 2011-08-08
            • 1970-01-01
            • 1970-01-01
            • 2011-08-12
            • 1970-01-01
            相关资源
            最近更新 更多