In my app I am applying push notifications and making them using the notificationCompat class.
I am assigning each notification a unique ID therefore, each one is visible seperately in the notification bar.
When the user clicks on a notification, using a pending intent, I am directing the user towards a certain activity, where he can see all the previous notifications. ( I read them from mysql db ).
question 1 : I want all the notifications in the status bar get cleared if the user clicks only on one of them. Can be a any random one (given it is from my app). the setAutoCancel() method, only removes the one pressed, and I cant seem to know where I would implement the cancelAll() method.
Answer :
#Override
protected void onResume()
{
super.onResume();
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancelAll();
}
question 2 : Since each notification has unique ID, sending several notifications from an individual app, and in case of not checking from the user, might result in several icons to appear in the notification bar. Android, starts collecting them and showing badge numbers on them once they start taking too much space and the system realizes there is no enough space for them.
Is there any method to combine them all from the beginning ? That is, whenever I send a notification, and it is not checked, the second one should be added and a single icon should appear on the bar with the number 2 or + , whatever the system uses.
I'd like to combine question 1 and 2 into a single answer: Do not show multiple notifications! Show one notification and update it when you have more than one unchecked notification. As a user I would get really annoyed if my notification bar was spammed with notifications from a single app. Think of the GMail app and it's notifications if you would get one per incoming e-mail.
On pre 4.1 devices you'll have to make do with the Normal View. If you have only one unchecked message you could show the contents of the message immediately in the notification, and if you have more than one you could do something like the GMail notification and show something like "5 unchecked messages".
On 4.1+ devices you can use the Big View to show all of them at once.
Related
I'm creating a very simple application for myself. Basically, it gives me a certain notification exactly every 2 hours.
The problem is when I don't "check" the notification for 2 hours and the next notification is supposed to come around. It sends a notification even though another notification already exists.
I'm simply sending notifications with NotificationManager.
Is there a way to check if previous notification already exists and only send another one if it doesn't?
Assuming you are using the same notification id (so that only one notification appears in the notification tray), you can use setOnlyAlertOnce(true) to ensure that sound/vibrate only plays the very first time a notification is posted and not when an existing notification is updated.
My little app sends some notifications. We get a callback via a Pendingintent when the notification is clicked on. However, when a notification is simply removed without being clicked on, I don't get any kind of notification and thus wouldn't know if a notification has been removed by the user.
My ultimate goal is to limit the number of active notifications sent by my app to no more than 3. But I haven't been able to find a way to enumerate or simply get the count of active notifications sent by my app. The number of methods available in NotificationManager is rather limited.
Any help will be appreciated.
You can set a PendingIntent with setDeleteIntent() which will be called when the notification is removed from the notification tray (such as when the user swipes to dismiss it).
Do note that the notification design guidelines state:
If a notification of a certain type is already pending when your app tries to send a new notification of the same type, combine them into a single summary notification for the app. Do not create a new object.
A summary notification builds a summary description and allows the user to understand how many notifications of a particular kind are pending.
I.e., don't do this:
Do this (this example uses an InboxStyle notification as is recommended):
Make sure you are not posting multiple notifications of the same type.
the method "Notification.deleteIntent" you can use to set a PendingIntent which the notification was removed by system will be called .And then you can do something you want .
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
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!
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.
If you're targeting API level 18+, then you're in luck, as you can provide a NotificationListenerService 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 AccessbilityService and listening on AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED events. This is a good example of this approach.
My intention is to display a notification with a personalised icon. However, if a notification with the same id is already displayed and then I need to add a new notification with the same id, my program will instead prepare a "stacked" version of the notification where the icon is changed to some generic icon and the notification content shows excerpts from the last and current notifications. Similar to how Gmail does when there are multiple emails.
To implement that I need to check if there are notifications of my app, currently displayed. I do not see any API to retrieve my own notifications.
I cannot simply cache the notification details that I have displayed till now, since in that case I need to know when they will be dismissed by the user, and update my cache accordingly. I also do not see any API to listen for dismiss events.
If you observe the gmail app notification behaviour properly u will notice that even when u dismiss one notification gmail shows you the same notification again in the list when a new notification comes. It looks to me that gmail is relying on total unread/unopened messages rather than keeping a cache of notifications.
There is no direct api for ur suggested SDK version. You need to fallback to ur own implementation. However, there is a way to know if a already showed notification was dismissed - How to know when my notification is cleared via Clear button?
This is possible with android 4.3 upwards now
See http://developer.android.com/reference/android/service/notification/NotificationListenerService.html#getActiveNotifications()
My application displays event notifications, and I'm looking for a way to have notification aggregation.
Meaning, I would like to show 4 notification, but if the fifth comes, I would like to collect all notifications and show only one general notification.
Os there a way to know how many live notifications i have?
Is it possible to approach these notification and cancel them?
Thanks!
According to the android design guidelines you should stack your notifications. So you should avoid showing multiple notification for the same app.
You can stack your notifications using
NotificationManager.notify(ID, notification) where you specify same ID for each notification.
You can check the docs on how to implement it.
For your case
Is there a way to know how many live notifications i have - No
But you can keep a track of the notifications using Shared Preferences, where you store the ID of the notificaion and remove it when the user clicks on the notificaion
Is it possible to approach these notification and cancel them? - Yes
You can cancel a notification using the cancel(int id) method, where you pass the ID of the notification to be removed.
So you can use this method to acheive what you want, but it advisable to stack all the notifications.