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!
Class to notify the user of events that happen. This is how you tell the user that something has happened in the background.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With