Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NotificationManager getActiveNotifications() for older devices

I want to be able to get active notifications from my Android app on demand. (actually I just need to know if there are any) I've been searching for this behavior and it seems, like I have only two options: NotificationManager.getActiveNotifications() which is exactly what I need, but is only available from SDK 23 or using a NotificationService but I really dislike this solution as I have to provide the permission to my app to read all notifications which is definitely an overkill.

Does anybody know about any solution which would behave like NotificationManager.getActiveNotifications() and not require SDK >= 23?

Thanks in advance!

like image 854
niosus Avatar asked Jan 24 '16 18:01

niosus


People also ask

What is the purpose of the Notificationmanager class?

Class to notify the user of events that happen. This is how you tell the user that something has happened in the background.

How do I turn on notification listener service?

To use NotificationListenerService, we need to create a java file which extends NotificationListenerService and implement two callback methods. Both methods have a parameter named “sbn”, which is an object of StatusBarNotification class. StatusBarNotification provides necessary information about Notifications.


1 Answers

You know which notifications you raised, because your code raised them.

In theory, you can know which notifications were removed, by using setDeleteIntent() to register a PendingIntent to find out when the user clears that notification. I have not used this in quite some time, so it is possible that this does not work as well as it used to.

But, if you know which notifications you raised, and you know which notifications were cleared, knowing which notifications are still outstanding is a matter of bookkeeping.

To be honest, "what are the active notifications?" feels like a code smell. There may be very good reasons for asking that question. If we were teammates, and this was part of code review, I would be asking why we care whether any given notification is active or not.

And, at this point, API Level 23 (Android 6.0) is six years old. You really should be reconsidering whether you should be supporting older versions of Android than that, given all the security problems that would remain on Android 5.x and older devices.

like image 57
CommonsWare Avatar answered Sep 23 '22 00:09

CommonsWare