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.
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.
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 .
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.
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.