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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3630733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:04:48  来源:igfitidea点击:

How to check which notifications are active in status bar in Android Dev?

android

提问by Yasir Malang

I have made an app that sets notifications in the drop-down status bar of Android phones. However, there is a bug in my code (sometimes the notifications are set, sometimes they are not). I want to be able TO CHECK (in the code) IF THE NOTIFICATION IS VISIBLE TO THE USER. (i.e. can the user see the notification in the status bar?).

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

How can I do this? (Thanks in advance).

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

Sample code is greatly appreciated.

非常感谢示例代码。

采纳答案by CommonsWare

I want to be able TO CHECK (in the code) IF THE NOTIFICATION IS VISIBLE TO THE USER. (i.e. can the user see the notification in the status bar?).

How can I do this?

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

我怎样才能做到这一点?

You can't, sorry.Update: Now possible with Android 4.3+ http://developer.android.com/reference/android/service/notification/NotificationListenerService.html#getActiveNotifications()

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

However, you can always simply cancel()it -- canceling a Notificationthat is not on-screen is perfectly fine. Conversely, you can always safely call notify()again for the same Notification, and it too will not cause a problem if the Notificationis already on-screen.

但是,您始终可以简单地进行操作cancel()——取消Notification不在屏幕上的一个完全没问题。相反,您始终可以安全地notify()再次调用相同的Notification,如果Notification已经在屏幕上,它也不会引起问题。

EDIT:

编辑:

NotificationManager.getActiveNotifications()was added in API 23 if you don't want to use the NotificationListenerService

如果您不想使用NotificationManager.getActiveNotifications()已在 API 23NotificationListenerService

回答by Naren Neelamegam

Just to put all together. This is how it works

只是把所有东西放在一起。这是它的工作原理

To build a notification,

要构建通知,

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

To make a notification sound call setSound()of Notification,

要发出通知的通知声音呼叫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();

To cancel the notification after user selected and launched the receiver Intent, call setAutoCancel(),

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

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

To make sound/vibrate only once for a particular notification use Notification.FLAG_ONLY_ALERT_ONCE. With this flag, your notification will make sound only once till it gets cancelled and you can call notify() as many times as you want with the notification id. Note that if you call cancel() or if user cancelled the notification or auto cancelled, notify() call will make the notification sound again.

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

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

Finally to put the notification on notification panel,

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

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        notificationManager.notify(notification_id, n);

Note that notification_idhere is important if you want to use the notification effectively.( to keep single sound/vibration for a notification or to cancel a specific notification).

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

To cancel a particular notification,

要取消特定通知,

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

You can cancel()a notification even if it doesn't exist or you can call notify()as many times as you want with the same id. Note that calling notify with different id will create new notifications.

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

So, regardless of whether the notification exist or not, if you call notify()again with the correct notification_idwith the Notification.FLAG_ONLY_ALERT_ONCEflag set, you can keep your notification alive without disturbing the user with repeated sounds.

因此,无论通知是否存在,如果您在设置标志的情况下notify()以正确的方式再次调用,您可以保持通知有效,而不会因重复的声音打扰用户。notification_idNotification.FLAG_ONLY_ALERT_ONCE

回答by weakwire

You need to set an id for each notification you make.

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

so you make a notification ..

所以你做一个通知..

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);

to clear it..

清除它..

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

and to check if active..( existAlarm returns null if no pending intent available)

并检查是否处于活动状态..(如果没有可用的待处理意图,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;
 }

So everything comes down to initialize an ID for each notification and how you make it unique.

因此,一切都归结为初始化每个通知的 ID 以及如何使其独一无二。

回答by dev.bmax

A new method is introduced to the NotificationManager class in API 23:

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

public StatusBarNotification[] getActiveNotifications()

回答by Abhishek Saini

There exists a flag for that.

有一个标志。

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

FLAG_ONLY_ALERT_ONCE:

FLAG_ONLY_ALERT_ONCE:

...should be set if you want the sound and/or vibration play each time the notification is sent, even if it has not been canceled before that.

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

Although, the notification willblink when it is sent again, but there won't be any sound or vibration.

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

回答by VitalyB

It seemsthat from Android M (API 23) it is possible to get your process like that, without using NotificationListenerService nor requiring additional permissions:

似乎是在Android M(API 23),可以让你的过程是怎样的,如果没有使用NotificationListenerService,也不需要额外的权限:

notificationManager.getActiveNotifications()

notificationManager.getActiveNotifications()

回答by IgorGanapolsky

As of Android Marshmallow (API 23), you can recover a list of active notifications posted by your app. This NotificationManagermethod is getActiveNotifications(). More info here: https://developer.android.com/reference/android/app/NotificationManager.html#getActiveNotifications()

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