English 中文(简体)
How to collapse Android notifications?
原标题:

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





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签