Search code examples
androidpush-notificationandroid-c2dm

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!


Solution

  • 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);