SayantanRC
SayantanRC

Reputation: 949

Correct way to clear android notification actions in android P?

I have an ongoing notification with a "Cancel" action. It looks something like this:

NotificationCompat.Builder progressNotif = new NotificationCompat.Builder(context, BackupService.BACKUP_RUNNING_NOTIFICATION);
...
...
// adding an action
progressNotif.addAction(cancelAction);

Next, after the job is over, I want to remove the "Cancel" action. What I was trying was this:

progressNotif.mActions.clear();

But recently, Android Studio is giving me this error:

Builder.mActions can only be accessed from within the same library group (groupId=com.android.support)

This will probably cause a problem in Android Pie and above. What does this error mean? How do I resolve it?

Upvotes: 2

Views: 1306

Answers (2)

DannyRitiu
DannyRitiu

Reputation: 91

If you are using Kotlin, you can extend the NotificationCompat.Builder class like this:

@SuppressLint ("RestrictedApi")
fun NotificationCompat.Builder.clearActions () {
    mActions.clear()
}

Then you can just simply call

progressNotif.clearActions()

Upvotes: 1

VIN
VIN

Reputation: 6957

If you have the notification ID, you can call cancel with the notification manager.

final NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(notificationId);

Upvotes: 0

Related Questions