Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to properly clear all notification once clicked?

I send a few notification on the notification bar, i wanted to clear all of it when one of the notification is clicked. For now I clear one by one by using Flag. I know notificationManager.cancelAll() could clear all the notification but where should i put so that i can trigger once one of the notification is clicked.

 private static void generateNotification(Context context, String message) {     int icon = R.drawable.ic_launcher;     long when = System.currentTimeMillis();     NotificationManager notificationManager = (NotificationManager)             context.getSystemService(Context.NOTIFICATION_SERVICE);     Notification notification = new Notification(icon, message, when);     String title = context.getString(R.string.app_name);      Intent notificationIntent = new Intent(context, MainActivity.class);      // set intent so it does not start a new activity     notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |             Intent.FLAG_ACTIVITY_SINGLE_TOP);     PendingIntent intent =             PendingIntent.getActivity(context, 0, notificationIntent, 0);     notification.setLatestEventInfo(context, title, message, intent);     notification.flags |= Notification.FLAG_AUTO_CANCEL;      notificationManager.notify(msgid, notification);       //notificationManager.cancelAll(); //i wan to clear all when the notification is clicked, where should i put this line? } 
like image 662
Eric Avatar asked Nov 30 '12 10:11

Eric


People also ask

How do I delete all my notifications at once?

Use notifications To clear one notification, swipe it left or right. To clear all notifications, scroll to the bottom of your notifications and tap Clear all.

How do I manage multiple notifications on Android?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


1 Answers

My solution is to call it at onResume().

@Override protected void onResume() { super.onResume();  // Clear all notification NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); nMgr.cancelAll(); }  
like image 60
Eric Avatar answered Oct 05 '22 15:10

Eric