[ACCEPTED]-Android - Remove action button from notification-notifications

Accepted answer
Score: 29

If you are using the NotificationCompat.Builder 5 from the v4 Support Library, you can simply 4 access the builder's action collection directly 3 (Unfortunately no public mutators are provided).

The 2 following will do the trick (Of course you 1 must update re-notify):

NotificationCompat.Builder notifBuilder = NotificationCompat.Builder(context);
...
notifBuilder.mActions.clear();
Score: 3

I am using following workaround:

NotificationCompat.Builder builder = //existing instance of builder
//...
try {
    //Use reflection clean up old actions
    Field f = builder.getClass().getDeclaredField("mActions");
    f.setAccessible(true);
    f.set(builder, new ArrayList<NotificationCompat.Action>());
} catch (NoSuchFieldException e) {
    // no field
} catch (IllegalAccessException e) {
    // wrong types
}

from here: https://code.google.com/p/android/issues/detail?id=68063

Note: Proguard 3 may break the button clearing in obfuscated 2 build. Fix is to add the following two lines 1 in proguard-rules.pro

-keep class androidx.core.app.NotificationCompat { *; }
-keep class androidx.core.app.NotificationCompat$* { *; }
Score: 2

I had the same problem and found a solution 5 for this. I created another builder and 4 added two "empty" actions like this:

builder.addAction(0, null, null);
builder.addAction(0, null, null);

(one 3 for each button I had, so if you have three, call 2 it three times).

Then when calling Notify, it 1 removes the buttons.

Score: 1

Even though the accepted answer works, as 3 per documentation, the designed way to do 2 this is by using NotificationCompat.Extender class. For example in 1 Kotlin:

private val clearActionsNotificationExtender = NotificationCompat.Extender { builder ->
    builder.mActions.clear()
    builder
}

private val notificationBuilder by lazy {
     NotificationCompat.Builder(context)
           .addAction(R.drawable.ic_play_arrow, "Play", playPendingIntent)
}

private fun updateNotification(){
     notificationBuilder
          .extend(clearActionsNotificationExtender) // this will remove the play action
          .addAction(R.drawable.ic_pause, "Pause", pausePendingIntent)
}

More Related questions