devanshu_kaushik
devanshu_kaushik

Reputation: 979

Is there any way to find out the number of active notifications from my Android App?

I am working towards managing the notifications that my app creates in the Android device. Is there any method that can provide me the number of notifications from my app that are active (i.e. still visible in the notification drawer) at any given time?

Upvotes: 1

Views: 2894

Answers (3)

SilverTech
SilverTech

Reputation: 429

fun getNotificationCount(): Int {
    return notificationManager.activeNotifications.size
}

Upvotes: 0

harsh201
harsh201

Reputation: 121

You can use NotificationManager for getting list of all the active notifications posted by your application using getActiveNotifications()

As per the reference doc for getActiveNotifications():

Recover a list of active notifications: ones that have been posted by the calling app that have not yet been dismissed by the user or cancelled by the app.

Upvotes: 4

dabluck
dabluck

Reputation: 1181

to expand on commonswares comment:

I see two approaches here:

1) manage this number via a count sharedpreferences or a database, etc. you will need to supply a deleteIntent which starts something to update this number when they dismiss the notification and a contentIntent for when they open the notification (this will also update your count).

2) read this number explicitly from a notification listener service.

https://developer.android.com/reference/android/service/notification/NotificationListenerService.html

#1 is much preferred because the user doesn't have to opt in to the behavior and asking for their full notification list is entirely unnecessary.

Upvotes: 1

Related Questions