简体   繁体   中英

Android - Remove action button from notification

I want to dismiss the notification action buttons (not the whole notification) when clicking on those action buttons. (Lets say: a download notification with stop action button. When click on stop, dismiss stop button and change contentText to 'Download canceled')

The only thing it comes to my mind is to cancel notification and notify another one with the same id, but this seems to be an ugly workaround...

So, is there any way to remove action buttons from notifications?

(i think there is no need to put any code, but I will if its necessary...)

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

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

NotificationCompat.Builder notifBuilder = NotificationCompat.Builder(context);
...
notifBuilder.mActions.clear();

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 may break the button clearing in obfuscated build. Fix is to add the following two lines in proguard-rules.pro

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

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

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

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

Then when calling Notify, it removes the buttons.

Even though the accepted answer works, as per documentation, the designed way to do this is by using NotificationCompat.Extender class. For example in 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)
}
NotificationCompat.Builder notifBuilder = NotificationCompat.Builder(context);

remove Whole Action Button :

builder.mActions.clear();

for remove special action button :

builder.mActions.remove(index);

finally :

notificationManager.notify(notificationID, builder.build());

Android provides the notification area for alerting users about the events that have occurred. It also provides a notification drawer that user can pull down to see more detailed information about the notification.

Notification Drawer consists of

  • View (contains tittle,detail,small icon)
  • Action ( any action which may occur in case the user clicks the notification drawer view)

To set up a notification so it can be updated, issue it with a notification ID by calling NotificationManager.notify(ID, notification). To update this notification once you've issued it, update or create a NotificationCompat.Builder object, build a Notification object from it, and issue the Notification with the same ID you used previously.

mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    // Sets an ID for the notification, so it can be updated
    int notifyID = 1;
    mNotifyBuilder = new NotificationCompat.Builder(this)
    .setContentTitle("New Message")
    .setContentText("You are downloading some image.")
    .setSmallIcon(R.drawable.ic_stop)
   numMessages = 0;  
  // Start of a loop that processes data and then notifies the user
  ...
  mNotifyBuilder.setContentText("Download canceled")
    .setNumber(++numMessages);
  // Because the ID remains unchanged, the existing notification is
  // updated.
  mNotificationManager.notify(
        notifyID,
        mNotifyBuilder.build());
  ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM