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

匿名 (未验证) 提交于 2019-12-03 01:48:02

问题:

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?).

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

Sample code is greatly appreciated.

回答1:

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

However, you can always simply cancel() it -- canceling a Notification that 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 Notification is already on-screen.

EDIT:

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



回答2:

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,

        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(),

        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.

        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_id here is important if you want to use the notification effectively.( to keep single sound/vibration for a notification or to cancel a specific notification).

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.

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



回答3:

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

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, , System.currentTimeMillis()); NotificationManager manger = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); notification.setLatestEventInfo(context,  + b.getString("channel"),  + showname, pendingIntent);        
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!