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 .
Related
I'm using Google Cloud Messaging to receive new orders into an app. I'm trying to handle cases where the same order is sent twice. I just want the second receipt to be ignored, unfortuntately when the app is in the background I dont seem to be able to cancel the notification (ie it still makes a noise and sends a message). The app works fine when in the foreground, putting cancel notification code in my GCMBrodacastreceiver doesnt seem to do anything. Am I missing something?
NotificationManager mNotify = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
mNotify.cancelAll();
You can set the "tag" field in the notification payload. If you use this the incoming notification will just update any existing one with the same tag.
cancelAll() will dismiss the notification, but your code may not be invoked when the app is in the background as the notification will be posted without your app's code running. One option would be to have your server not send the notification if it has already sent one recently.
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.
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.
Is it possible for me to get all of the notifications from my app that's currently in the system tray?
I noticed we can update existing notifications that are already in the tray, but I am wondering if it's possible for me to retrieve all of them.
http://developer.android.com/guide/topics/ui/notifiers/notifications.html#Updating
Thanks
The short answer is "No", the Notification Manager does not provide a way to query the notifications currently in the tray.
If you want to keep track of your notifications in the tray, it is possible, but will take a little bit of work. You will need to create a way to track the status of your notifications, the easiest way is probably a database. What you do is insert a row for the notification, with that notification's id, when you create the notification. Next, you need to build an Activity or Service that will update your database with the status of your notification during its lifecycle. Before posting your notification to the manager, you need to add a contentIntent and a deleteIntent that route through the Activity or Service you just created. The contentIntent will be called when the user clicks the notification, and the deleteIntent will be called if the user dismisses it. When either of those happen, update your database to track the latest state!
If you implement that, you'll have a way to track your notifications, and their status in the tray. Unfortunately, there is no easy, readily available way to query them, especially if they're not of your creation.
Try this NotificationListenerService:
https://developer.android.com/reference/android/service/notification/NotificationListenerService
Is there a way to first create a Notification and set some default message (the line below the title of a notification) and then to change that message every X seconds?
I tried finding solution, but those I found proposed constant killing and creating Notification which I do not find a proper way to achieve what I need.
I need to implement a small counter inside the same notification which will be updated every X seconds.
PS. Please do not confuse this with sending data from notification to an Activity. I need the reverse process - send data from activity to the notification.
Please have a look at android documentation
Updating notifications
To set up a notification so it can be updated, issue it with a
notification ID by calling NotificationManager.notify(ID,
notification). To update this notification once you've issued it,
update or create a NotificationCompat.Builder object, build a
Notification object from it, and issue the Notification with the same
ID you used previously. If the previous notification is still visible,
the system updates it from the contents of the Notification object. If
the previous notification has been dismissed, a new notification is
created instead.
The following snippet demonstrates a notification that is updated to
reflect the number of events that have occurred. It stacks the
notification, showing a summary:
Probably setting a notification id and accessing the same notification by id is the solution to your problem. Here is the link to document.
Edit:
Probably I missed one aspect of your question - i.e. creating new instance of notification. To resolve it you can declare an instance of notification in a singleton class. This way, you will not have to create an instance of notification again and again. Just modify the content of notification and use the same instance again and again.