7

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.

2 Answers 2

11

Use below code -

NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.cancelAll();
6
  • 1
    where should i have to use this?? Feb 2, 2016 at 10:19
  • I am using NotificationCompat.Builder Feb 2, 2016 at 10:20
  • @Demo Mail: You can use above code inside oncreate(). No issues in NotificationCompat.Builder.
    – kevz
    Feb 2, 2016 at 10:21
  • I used Notification Listener to clear the notifications Jun 19, 2018 at 6:17
  • @nmanikumar5:You can use NotificationManager to cancel notification of your app only. While NotificationListenerService can clear others apps notifications as well
    – kevz
    Jun 19, 2018 at 7:01
9

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();
  }
4
  • I don't want to click on notification, I just directly open my app, then automatically will have to clear all the notification of my app. Feb 2, 2016 at 11:27
  • @DemoMail call notificationManager.cancelAll(); in Oncreate method of Acivity, all notification will be cleared without click on a notification. Feb 2, 2016 at 11:33
  • onStart would be better than onCreate Feb 2, 2016 at 11:40
  • @KapilRajput & EugenPechanec I will check both solutions and reply Feb 3, 2016 at 2:59

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.