Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to clear all notification without clicking on notification in android

Plz don't mark this as duplicate.

I am working on notification and it is working fine. But what I want is that when notification appears, can we clear all the notification without clicking on the notification, means that suppose if I get 5 notification and when I simply open my app then that notification should have to removed without clicking on that notification.

Can we do this??

I have searched and all says to use setAutoCancel(true). I have already used this.

like image 294
Mohd Asif Ahmed Avatar asked Feb 02 '16 10:02

Mohd Asif Ahmed


People also ask

How do I clear all notifications on Android?

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.


2 Answers

Use below code -

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancelAll();
like image 113
kevz Avatar answered Oct 25 '22 21:10

kevz


If you want to just clear Notifications according to id's do it this way,

while notifiy notification from NotificationManager set a tag to your nofification's and save id's of notification's in a Arraylist like this

static ArrayList<String>notifIds=new ArrayList<>();

//your code


notificationManager.notify("myappnotif",NOTIFICATION_COUNT, builder.build());
        notifIds.add(data);

now to remove Notifications call notificationManager.cancel(String tag, int id) wherever you want like this

  NotificationManager notificationManager = (NotificationManager)  getSystemService(NOTIFICATION_SERVICE);
    for(int i=0;i<MainActivity.notifIds.size();i++){
     notificationManager.cancel("myappnotif", i);
     }

and to remove all Notification's when application start's call notificationManager.cancelAll(); in onCreate of Launcher Activity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
     notificationManager.cancelAll();
  }
like image 33
Kapil Rajput Avatar answered Oct 25 '22 22:10

Kapil Rajput