0

I'm trying to create a stack notification in the action bar. I want to update the counter (++counter) when a new notification arrives and show the counter value in the action bar. like this: "2 new messages"

My problem is that when the user has already clicked on the notification message then it is deleted from the actionBar. Then later if a new notification appears I want my counter to reset and again show in the action bar the sum of messages that have not been read.

How can I know if the notification has already been removed/read from the action bar?

Toda
  • 403
  • 1
  • 6
  • 13

2 Answers2

0

This gets easier if you decide to reset the unread message counter when the user reads the messages. Now, you need to delete the old notification and make a new one, with the updated count.

That is, there's no need to check, just make a new notification and dismiss the old one: How to clear a notification in Android

Community
  • 1
  • 1
18446744073709551615
  • 16,368
  • 4
  • 94
  • 127
  • If I'm delete the notification where to reset the counter? I didn't understood how can I know if the user reads them for remove them? – Toda Mar 21 '16 at 14:23
  • You assume that the message is read when the user visits the screen (Activity) containing the messages. You reset the counter in _onResume()_. And if that Activity is on screen, you do not need to add notifications. – 18446744073709551615 Mar 21 '16 at 14:29
  • But I need that if the user is delete the message and doesn't visit in the activity the counter is still reset. – Toda Mar 21 '16 at 14:37
  • Then reset it when the user deletes the message, too. (Well, in this case it is more sensible to reset the counter if *either* the notification message is deleted *or* the activity containing the messages is launched -- I mean, it's meaningful to place both cases in the same class) – 18446744073709551615 Mar 21 '16 at 15:06
0

Are you targeting the latest APIs (21 or above?) Check for NotificationListenerService from Android Developers.

There are two methods, onNotificationPosted() and onNotificationRemoved()

Now, use a Set<notificationID> mySet. In onNotificationPosted(), call mySet.add() to add all the notifications posted.

In onNotificationRemoved(), call mySet.remove()

At any point when you want to check if notification is avaialble in notification bar, call mySet.contains()

Rusheel Jain
  • 843
  • 6
  • 20
  • How can I use Set? where to put this code? – Toda Mar 21 '16 at 14:27
  • Set where T is Generics Type. So use a Set like HashSet etc. or any other Collections that you might be interested in. In onNotificationPosted(StatusBarNotification sbn) method, keep on add the notification id that was posted i.e. something like mySet.add(sbn.getId()) Look here http://developer.android.com/reference/android/service/notification/NotificationListenerService.html – Rusheel Jain Mar 21 '16 at 14:30
  • Do you mean that I need to create some database to save the notification that I notifyed? and then I can access to each of them? – Toda Mar 21 '16 at 14:41
  • I need to create class and extand NotificationListenerService, then in the code can I check if they read? – Toda Mar 21 '16 at 14:43
  • @Toda please read that link. This is a listener service that will help you to "listen" to the notifications that are posted in your notification bar, either from your own app or some other app like whatsapp. Now whenever user will read/delete etc. the notification, you will be notified again because of this service. – Rusheel Jain Mar 21 '16 at 14:44
  • can you give me an example link? I didn't understood how to use this. thank you! – Toda Mar 21 '16 at 15:41
  • I don't targeting the latest APIs, what can I do? – Toda Apr 06 '16 at 07:39