1477

How to get notification count in android?

Question:

I have a custom launcher, and I'm showing a custom notification icon If user clicks on the notification icon he gets to see the notification. This part is working as expected. I can expand the notification list.

But now my requirement is, since I'm using a custom Notification icon I wish to show the notification count(if there are any notification, or if there are 10 notifications). I wish to show the number of unread/unchecked notification user has.

How to get the number(count) of unchecked notification?

I have gone through couple of examples and link like:

<a href="http://www.tutorialspoint.com/android/android_notifications.htm" rel="nofollow">Link 1</a>

<a href="http://developer.android.com/guide/topics/ui/notifiers/notifications.html" rel="nofollow">Link 2</a>

But all these links show how to create notifications, or how to expand notification list. How to get the notification count?

Any piece of code or example is highly appreciable.

Thanks

Answer1:

you can use this exemple it work for me

Notification notification = new Notification(R.drawable.direction, "Cool Notification", System.currentTimeMillis()); /********LIKE THIS*********/ notification.number = notificationCount++; /********LIKE THIS*********/ notification.setLatestEventInfo(context, "Cool Notification Title", "updated notificaiton message", null); NotificationManager nm = (NotificationManager) context.getSystemService(context.NOTIFICATION_SERVICE); nm.notify(R.id.my_motification, notification);

So you can increment the notification.number every time you display a notification.

Answer2:

If you're targeting API level 18+, then you're in luck, as you can provide a <a href="https://developer.android.com/reference/android/service/notification/NotificationListenerService.html" rel="nofollow">NotificationListenerService</a> to access the current notifications and/or be alerted whenever they are created or dismissed.

Otherwise, you can use the old trick of registering an <a href="http://developer.android.com/reference/android/accessibilityservice/AccessibilityService.html" rel="nofollow">AccessbilityService</a> and listening on AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED events. <a href="https://stackoverflow.com/questions/22286681/reading-notifications-using-accessibilityservice" rel="nofollow">This</a> is a good example of this approach.

Answer3:

I would like to share my strategy on how to get the notification count. As I read the documentation, I am have not seen any way to store, retrieve the notification count. I am doing a Note Reminder and this reminder alarms at a particular date. Given I have many reminders, sending notification to each of them simply replaces the one on the notification list. This is not nice. Neither my receiver has any way to know the nth time the notification was called. The notification is lack-luster in this case in my opinion.

So, one strategy I saw is to defer the counting responsibility directly to the database. I have to provide a method which returns the number of lapsed reminders I have on the database, tuck it in as an extra on the intent inside the pending intent which launch my receiver. From there I can unpack the extras and set the notification accordingly. This way, I got the information I need and I can model the notification content as such showing the number of reminders that are left untouched. Adding a count badge right next to the icon does not seem possible given the default android but is possible using third-party solutions or using well known android UI like TouchWiz in Samsung, or the one in Xperia. Searching the web, it seems making it so is another different story. I preferred not to do it and simply show through the content I have n count of lapsed reminders.

I hope this help people who are having similar problem regarding retrieving, or storing notification count given a unique-per-application notification id.

Hope this helps!

Recommend

  • How do you change the output volume of a sound?
  • Ongoing Notification Disappears When Clearing Memory from the device in android?
  • Close android Notification
  • Open Gallery Folder By Click On Notification
  • how to awake already running app after c2dm message on android
  • sms notification- not working
  • Cannot Send data Through pendingIntent
  • Android notification manager update percentage
  • Notification in all devices
  • Creating custom notification with xamarin.android
  • App it not doing…anything?
  • send data from notification to activity and make activity behave accordingly
  • Get functions names in a shared library programatically
  • Android Inside Custom ListView, TextView Weight property not working
  • How to get notification count in android?
  • Why git pull --rebase results a forced update?
  • Android Multiple Instances
  • Incorrect timestamp on future notifications
  • Firebase fcm token not generating
  • Group ID being ignored when creating NotificationChannel
  • pyspark streaming restore from checkpoint
  • Notification Icon showing white on lollipop and above devices
  • get details of source repository from Jenkinsfile
  • Android: Transfering Data via ContentIntent
  • Titanium - Perform Action when user clicks on Android Notification
  • Can weak symbol be resolved among libraries during linking?
  • ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS in not firing
  • Android sensor listener does not get unregistered or sensor still retrieves values
  • Push notifications don't work
  • AlarmManager is blocking main thread
  • Why is my AlarmManager firing off instantly?
  • onLeScan never gets called android
  • Missing permission to control media error on Android 5/L
  • how to adjust image in a panel in Java swing?
  • Encrypt data by using a public key in c# and decrypt data by using a private key in php
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Linker errors when using intrinsic function via function pointer
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • LevelDB C iterator
  • How can i traverse a binary tree from right to left in java?