NotificationManager getActiveNotifications() for older devices
Asked Answered
V

2

14

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!

Varistor answered 24/1, 2016 at 18:15 Comment(1)
@Nitesh, a way out - yes, a solution - no. I ended up just storing the active notification ids by myself. Please thumb up my question if you are interested - maybe someone will eventually stumble upon it.Varistor
Y
1

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.

Youthen answered 26/2, 2021 at 13:57 Comment(0)
E
0

In my project, I used a static array to save all pending notifications. This array will be cleared when the app come onto foreground. Thanks.

Embolic answered 5/8, 2016 at 8:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.