3

Here is the code that I'm using to create a notification in Android. This notification is shown as intended and isn't getting removed by swiping the notification. But I'm unable to remove it programmatically. How should I remove it?

NotificationCompat.Builder mBuilder =
            new NotificationCompat.Builder(getApplicationContext(), Constants.CHANNEL_ID)
                    .setSmallIcon(R.drawable.small)
                    .setContentTitle("persistent")
                    .setContentText("cant remove").setOngoing(true);
    mBuilder.build();
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Shobhit Kumar
  • 626
  • 1
  • 5
  • 21
  • Possible duplicate of [How to remove notification from notification bar programmatically in android?](https://stackoverflow.com/questions/19268450/how-to-remove-notification-from-notification-bar-programmatically-in-android) – Cătălin Florescu Oct 18 '18 at 13:59

1 Answers1

4

In this answer there's a nice explanation on how to do it: https://stackoverflow.com/a/19268653/3853450

In your case should be something like this:

NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(Constants.CHANNEL_ID);

Also, as given in the original answer:

NotificationManager

Archison
  • 81
  • 5