如何检查Android Dev中状态栏中哪些通知处于活动状态?

Yas*_*ang 44 android

我制作了一个应用程序,可以在Android手机的下拉状态栏中设置通知.但是,我的代码中存在一个错误(有时会设置通知,有时则不会).我希望能够检查(在代码中)如果通知对用户是可见的.(即用户可以在状态栏中看到通知吗?).

我怎样才能做到这一点?(提前致谢).

非常感谢示例代码.

Com*_*are 41

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

我怎样才能做到这一点?

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

但是,你总是可以简单地cancel()取消它 - 取消Notification不在屏幕上的那个是完全没问题的.相反,您可以随时安全地notify()再次呼叫Notification,如果Notification已经在屏幕上,它也不会导致问题.

编辑:

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

  • 它重新振动并发出声音.那有什么办法吗? (3认同)
  • 很糟糕,我无法获得通知的状态.由我自己的应用程序发布而不注册`NotificationListenerService`(需要获得通知的权限). (3认同)

Nar*_*gam 30

只是把所有在一起.这是它的工作原理

要建立通知,

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

要发出通知声音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();
Run Code Online (Sandbox Code Playgroud)

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

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

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

        n.flags |= Notification.FLAG_ONLY_ALERT_ONCE;    // Dont vibrate or make notification sound
Run Code Online (Sandbox Code Playgroud)

最后将通知放在通知面板上,

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        notificationManager.notify(notification_id, n);
Run Code Online (Sandbox Code Playgroud)

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

要取消特定通知,

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.cancel(notification_id);
Run Code Online (Sandbox Code Playgroud)

你可以cancel()通知,即使它不存在,或者您可以拨打notify()只要你想用同一ID多次.请注意,使用不同ID调用notify将创建新通知.

因此,无论通知是否存在,如果您notify()再次使用正确notification_idNotification.FLAG_ONLY_ALERT_ONCE标志设置进行呼叫,您可以保持通知的有效性而不会因重复声音而打扰用户.

  • 对于NotificationCompat,`.setOnlyAlertOnce(true)` (4认同)

wea*_*ire 27

您需要为每个通知设置一个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);
Run Code Online (Sandbox Code Playgroud)

清除它..

PendingIntent pendingIntent = PendingIntent.getBroadcast(context,notId, intent, 0); 
pendingIntent.cancel();
Run Code Online (Sandbox Code Playgroud)

并检查是否有活动..(如果没有可用的待处理意图,则existsAlarm返回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;
 }
Run Code Online (Sandbox Code Playgroud)

因此,所有内容都归结为初始化每个通知的ID以及如何使其独特.

  • @CommonsWare:如果我调用具有待定意图的通知,我可以检查Pending意图是否存在,那么通知也存在 (10认同)
  • 那个存在警报的"选定位置"是什么? (6认同)

dev*_*max 6

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

public StatusBarNotification[] getActiveNotifications()
Run Code Online (Sandbox Code Playgroud)

  • 我期待在2019年使用它. (63认同)
  • 我们在PRE - Marshmallow版本(API 23)上有解决方案吗? (2认同)

Abh*_*ini 5

有一个标志。

Notification notification = new Notification(icon, tickerText, when);
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
Run Code Online (Sandbox Code Playgroud)

FLAG_ONLY_ALERT_ONCE:

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

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