Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...)

like image 550
BamsBamx Avatar asked May 28 '14 22:05

BamsBamx


People also ask

How to remove action button from notification in Android?

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.

How Do you handle opening activity using notifications?

Build and issue the notification: Create an Intent that starts the Activity . Set the Activity to start in a new, empty task by calling setFlags() with the flags FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TASK . Create a PendingIntent by calling getActivity() .

What are heads up notifications?

A notification is a message that Android displays outside an app to provide updates, reminders, and other timely information to users. In the Android Automotive OS, a notification can be displayed either as a Heads-Up Notification (HUN) or in the Notification panel (or in both).

What is notification icon in Android?

A notification is a message that Android displays outside your app's UI to provide the user with reminders, communication from other people, or other timely information from your app. Users can tap the notification to open your app or take an action directly from the notification.


2 Answers

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();
like image 171
coxc Avatar answered Oct 12 '22 23:10

coxc


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$* { *; }
like image 23
Bhiefer Avatar answered Oct 12 '22 22:10

Bhiefer