Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to remove a persistent notification after setongoing(true)?

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();
like image 942
Shobhit Kumar Avatar asked Jun 10 '26 04:06

Shobhit Kumar


1 Answers

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

like image 73
Archison Avatar answered Jun 16 '26 04:06

Archison



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!