I am polling from a server every x minutes to get events a user is invited to. Let's say the user has a new invite and gets a notification at 10:00 am. The user never clicks on the notification, and I want to prevent the user from receiving another notification x minutes later with the same information. Basically I want to allow unique notifications to only display once and alert the user once.
You would think this line of code would work do what I want desired, but it does not.
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
Anyone have any ideas?
I'm not certain if your problem is that you are seeing multiple notifications or just multiple sounds/vibrations are happening. Two things you need to do:
Yes use the Notification.FLAG_ONLY_ALERT_ONCE or NotificationCompat.Builder#setOnlyAlertOnce(), this will prevent sounds and vibrations from occurring when you update the notification
To prevent multiple notifications from appearing in the notification area you need to make sure that when you call NotificationManager#notify(id, builder.build()); the id is a consistent value that is always the same for that particular event.
Related
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 found one answer here:
Pin Notification to top of notification area
Does anyone known what the Vaiden's answer means?
notification.when = previousTimestamp;
How to get the "previousTimestamp"?
Thanks.
Vaiden means to make up timestamps for all of your notifications in the order you want them to appear in. Give a more recent timestamp for the ones that you want to have appear up top. The key point is that in the following example, previousTimestamp does not change, as opposed to constantly updating it with the current system time on every refresh (in the case of the question you are referring to, every 3 seconds).
set the following on all of your notifications:
myNotification.when = previousTimestamp;
not
myNotification.when = System.currentTimeMillis();
EDIT: Besides Vaiden's method, there are two things you can do to move your notifications up.
You can set the following two flags on your notification to make it ongoing, keeping it above non-ongoing notifications. Keep in mind that notifications that the user would expect to be able to clear, such as an email notification, should not use this method.
myNotification.flags |=
Notification.FLAG_ONGOING_EVENT;
myNotification.flags |= Notification.FLAG_NO_CLEAR;
You can set the notification priority to a higher level of you are developing for Android 4.1 Jelly Bean and above.
They are MAX, HIGH, DEFAULT, LOW, and MIN. If your app runs below Jelly Bean as well, then the priority is just default. See http://developer.android.com/design/patterns/notifications.html for more details on what priority is appropriate for which type of notification, but just remember, as with the ongoing flag, don't abuse the high priority setting.
NotificationCompat.setPriority(PRIORITY_MAX);
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.
I am working on a chat app in Android using c2dm (gcm). Every received message raises a notification if my app is not able to show it directly. The problem is when the user is off the grid, in which case c2dm messages stack up and get delivered all at once when he is back online. A cacaphony of notification sounds is the result from adding all the notifications in a 1 second period. How to prevent this? Desired behaviour is a that the notification sound is played once, while the tickertext and notification content are up to date with the last received message
I ended up using an alarm wich was set 1 second into the future. I set multiple alarms with the same intent, the earlier ones are automatically deleted. So when a second one comes in in the 1 second interval it deletes the first.
Utilize the collapse_key to only process the last "notification" of the stack
For more info, see https://developers.google.com/cloud-messaging/concept-options#collapsible_and_non-collapsible_messages
My app pops up a message count notification and sets it as "ongoing". There is a timer that re-sends the notification every 5 minutes, and the notification has the flag Notification.FLAG_ONGOING_EVENT , so that it can only get cleared by my app. And my app is set up to only use sound and vibrate if the count changes.
So, the idea is that if my app changes the count, it sends out an updated notification, which replaces the old one, and plays a sound and vibrates. But if the 5 minute timer tries to update the notification, but the count didn't change, the notification is still sent. but without sound or vibrate. This is done in case the notification somehow got cleared, I want it to pop back up, but if it's still there, I don't want the user to be re-notified.
I also save the message count so that it's remembered if the app is closed and then re-opened. The problem is that when that happens (if it's force-closed, for example), the old notification stays in the bar, but the newly opened app has no idea that's the case. So I'd like to be able to somehow poll the notification service to see if that original notification is still showing, but I can't find any API to do this. Is it possible?
Thanks.
So I'd like to be able to somehow poll the notification service to see if that original notification is still showing, but I can't find any API to do this. Is it possible?
No.
However, you can use deleteIntent to find out if the user cleared the Notification. Either that, or track down the cause of your "somehow got cleared" issue.