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.
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.
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.
Use below code -
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancelAll();
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();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With