Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get notification count in android?

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:

Link 1

Link 2

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

like image 506
Ari Avatar asked Jun 03 '14 14:06

Ari


People also ask

How do I count notifications on Android?

Right-click on drawable > new > vector asset > search for notification icon and select > finish. We are going to use this icon to show our notification count on top of it.

Why are my app badges not showing numbers?

If the badges are not displaying, you'll need to enable the notifications for that app and then turn on the app icon badges. Icon badges are only natively supported by the One UI Home launcher. If you are using an alternative launcher, badges may not be supported.

What is notification badge count?

Badge Count reflects all unread mail from your mail account(s), or just unread mail in your Focused Inbox.


1 Answers

I would like to share my strategy on how to get the notification count. As I read the documentation, I have not seen any way to store and 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!

like image 167
Neon Warge Avatar answered Oct 06 '22 20:10

Neon Warge