I'm sending a C2DM update to my Android app every 1/2 hour, which creates a Notification. Problem is, when I wake up in the morning I get 15 Notifications queued up in the status bar.
How do I only keep the latest notification, overwriting previous ones?
I tried looking at the C2DM documentation (http://code.google.com/android/c2dm/) which mentions a parameter called collapse_key, but I couldn't find an explanation for how to use it, nor am I sure the solution lies on the C2DM side.
Thanks!
If you want to cancel any previous notifications that has been set on the view you can try setting one of these flags.
PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_UPDATE_CURRENT
Something like this should replace your old notification i believe
NotificationManager mManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this,test.class);
Notification notification = new Notification(R.drawable.icon, "Notify", System.currentTimeMillis());
notification.setLatestEventInfo(this,"App Name","Description of the notification",
PendingIntent.getActivity(this.getBaseContext(), 0, intent, PendingIntent.FLAG_CANCEL_CURRENT));
mManager.notify(0, notification);
Notification has a property called number that shows a little number below the icon (for multiple notification). It lets you use the same Icon for Multiple Notification.
Use the same ID while updating your notification. :) Cheers.
In addition to the other answers, there is a parameter in your C2DM request that is called delay_while_idle. Make sure you are NOT including that or make it false. Your phone is "idle" when the screen is off (ie while you are sleeping). Google queues up all your messages on the server until the phone is not idle (ie when you turn on the screen in the morning). Then, Google sends all 15 messages at once and you display them at that time.
In the chrome to phone source, there is a method called sendNoRetry with this line:
if (delayWhileIdle) {
postDataBuilder.append("&")
.append(PARAM_DELAY_WHILE_IDLE).append("=1");
}
Make sure it is not true, then Google servers will send you your C2DM message every 30 minutes as expected.
collapse_id key should do the job. For updating any previous notification, just use the same key. To generate a new notification on device, use a different key.
For example,
* for chat notifications use the key "chat" (collapse_id = "chat")
* for invitations use the key "invite" (collapse_id = "invite")
So all the unqiue collapse_id notifications will group on device.
For more details visit: https://documentation.onesignal.com/reference#create-notification
Related
I'm using Firebase to store what I need for the notifications, and Google Cloud Functions to send the notifications. When a user decides to go to an app, they are subscribed to that event so that they can receive the chat notifications.
My first problem is that they are getting separate notifications for each message, and I imagine that would get pretty annoying in a group chat. I tried using collapseKey, but I think I'm either using it wrong or misunderstanding its purpose. Is it not working because the notification has already been sent? Either way, how do I fix this?
My second problem is that users are subscribed to an event, but because there's a bit of a delay in receiving the notification sometimes, they can get their own message notification if they exit the app before the notification is sent. How do I prevent this?
For your first problem,
use
Pending intent flags
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0 /* Request code */, intent,
PendingIntent.FLAG_UPDATE_CURRENT);
For more about flags Please visit this link
I couldn't figure out the PendingIntent solution, but here's what worked for me.
Turns out 'collapseKey' is for when devices are inactive. You can use 'tag' for when the phone is running to replace older notifications.
const payload = {
notification:{
title: eventName,
body: message,
tag: 'chat',
sound: 'default'
},
};
For the second part, I just ended up switching to sendToDevice instead of sendToTopic. I didn't know you could send it to more than one device at a time lol. Just a note, the array of registration tokens isn't allowed to be null.
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 .
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 receive Push-Notifications from GCM and want them to be shown in the Notification-Bar. So I generation the Notifications in the GCMIntentService-class.
If there is already a notification in the notification-area, this notification have to be appended with the new message (with use of the BigTextStyle). But how do I know if there is a notification from my app in the notification-area?
You cannot get existing notification, but you can update existing notification by setting up Notification ID.
NotificationManager.notify(NotificationId, NotificationObject);
No, you can't find out if there's already a notification posted; this is something you should keep track of in your app.
Fortunately, however, the API for updating an existing notification is identical to the API for creating a new one: notify(). That is, once you get new information, add it to some internal buffer (possibly just a StringBuilder) representing the complete set of received push notifications, then build a new notification with Notification.Builder and call notify() with the same ID and tag you used last time. Any existing notification with that ID and tag will be replaced with the new content.