How to count number of notification and display single icon in Android?
Asked Answered
B

3

11

I have multiple Android notification, but when I send a message from my web server, the android device creates a new notification icon on status bar. I want to count the number of unread notification, display it on statusbar with single icon, and when a notification is read, the notification has to change the number of unread notification count. How can I do it? It's look like "3 Others" in this image: Notification Icon

Buffet answered 17/6, 2011 at 3:54 Comment(0)
S
9

Check out the answer here: How to give counter if more than one Notifications are there

You just have to set Notification.number:

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);

You have to send your notification through the NotificationManager.notify method, with the same id all the time. As the documentation says, the id is a unique identifier for that notification within your application. If you reuse the same id, it will just update the text and number for that notification.

To check when the user clicks on the notification, you need to provide a PendingIntent (see the tutorial). To check when the user clears the notifications, you need to use the Notification.Builder that is only available in the Api Level 11.

Stymie answered 6/9, 2011 at 8:31 Comment(2)
Update : Now you can use NotificationCompat class in Android support package. developer.android.com/intl/es/reference/android/support/v4/app/…Westward
@BharathMg I cant't find number in NotificationxCompact!Rochelrochell
T
3

Well this codes work for me:

Notification n = new Notification(R.drawable.yourownpicturehere, 
                                   getString(R.string.noticeMe), 

System.currentTimeMillis());    
PendingIntent i=PendingIntent.getActivity(this, 0,
                                           new Intent(this, NotifyActivity.class),0);

n.setLatestEventInfo(getApplicationContext(), 
                     getString(R.string.title),
                     getString(R.string.message), i);
n.number=++count;
n.flags |= Notification.FLAG_AUTO_CANCEL;
n.flags |= Notification.DEFAULT_SOUND;
n.flags |= Notification.DEFAULT_VIBRATE;
n.ledARGB = 0xff0000ff;
n.flags |= Notification.FLAG_SHOW_LIGHTS;

// Now invoke the Notification Service
String notifService = Context.NOTIFICATION_SERVICE;
NotificationManager mgr = (NotificationManager) getSystemService(notifService);
mgr.notify(NOTIFICATION_ID, n);

Follow this link for complete tutorial.
I think the n.number=++count; is the one responsible for counting the notification

Tsana answered 14/6, 2015 at 12:22 Comment(0)
F
-1

You will need a different bitmap for each number, and set the icon to show the bitmap that corresponds to the number of notifications left.

To keep track of the notifications, you can simply set a counter in SharedPreferences, add to it with each new notification, and reduce by one when a notification is read (or to zero if you show all the notifications at once).

Fancywork answered 17/6, 2011 at 4:14 Comment(2)
@ Aleadam: I already add counter in SharedPreferences, but i don't know how to determine notification is readed or unread and set the value of counter? How can i catch this events?Buffet
there is a native way to do that with Android without using your own bitmaps. See #7204041Stymie

© 2022 - 2024 — McMap. All rights reserved.