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.
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.
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!