3

I would like to access the number of currently available push notifications The following :example has '1' push notification, here on a Android TV emulator image..

So how can I read that number?

EDIT: I do not want to set anything.

Ralf Wickum
  • 2,850
  • 9
  • 55
  • 103

2 Answers2

1

You can use NotificationListenerService to manage everything related to notifications. You can also extend it and override methods like onNotificationPosted to maintain your own counter. Or better yet, directly use getActiveNotifications().

Check the docs here https://developer.android.com/reference/android/service/notification/NotificationListenerService#getActiveNotifications()

You need to give your app permission to access notifications for this to work.

penguin2048
  • 1,303
  • 13
  • 25
  • Which exact permission? getActiveNotifications was always 0 in my tests – Ralf Wickum Mar 08 '21 at 10:12
  • Please check this example https://github.com/Chagall/notification-listener-service-example – penguin2048 Mar 09 '21 at 06:26
  • After you install the app ensure that permission is actually granted by going into app's settings -> permissions – penguin2048 Mar 09 '21 at 06:27
  • If getActiveNotifications is still not working maybe you can try to intercept notifications and maintain the counter yourself. – penguin2048 Mar 09 '21 at 06:28
  • 1
    Before calling [getActiveNotifications](https://developer.android.com/reference/android/service/notification/NotificationListenerService#getActiveNotifications()) make sure [onListenerConnected](https://developer.android.com/reference/android/service/notification/NotificationListenerService#onListenerConnected()) is called. – furkan Mar 09 '21 at 11:09
-1

You can set a Custom notification count.

By default, each notification increments a number displayed on the long-press menu (visible in figure 1), but you can override this number for your app.

For example, this might be useful if you're using just one notification to represent multiple new messages but you want the count here to represent the number of total new messages.

To set a custom number, call setNumber() on the notification - in here below

public NotificationCompat.Builder setNumber(int number)

Sets the number of items this notification represents. On the latest platforms, this may be displayed as a badge count for Launchers that support badging. Prior to Build.VERSION_CODES.O it could be shown in the header. And prior to Build.VERSION_CODES.N this was shown in the notification on the right side.

Please Refer this article https://developer.android.com/training/notify-user/badges