Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the action buttons on a notification

I have a notification that I'm trying to update by reusing the same Notification Builder, but there's no way to clear the buttons, you can only call addAction. Not using the same Builder results in the notification flashing, which is undesirable. Are there any solutions to this? I'm using NotificationCompat from the v4 support library.

like image 409
Jess Avatar asked Sep 04 '13 01:09

Jess


People also ask

How do I add buttons to my notifications?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml. Step 3 − Add the following code to src/MainActivity.

How do I handle action notifications on Android?

First build a Notification putExtra("action","actionName"); pIntentlogin = PendingIntent. getBroadcast(context,1,intentAction,PendingIntent. FLAG_UPDATE_CURRENT); drivingNotifBldr = (NotificationCompat. Builder) new NotificationCompat.


2 Answers

notificationBuilder.mActions.clear();

It's actually public ArrayList<Action>, so you can do whataver you want with it.

like image 81
Pitel Avatar answered Sep 27 '22 16:09

Pitel


You have two options to achieve that:

  1. Use a custom layout (just copy the design of the native notification if you want to) and then use this in a RemoteView and just make views visible or hide them. With remoteView.setViewVisibility(...) for example... Or change the text of the buttons...
  2. Use reflection to clear the builders actions. Would work like following:

    try {
        //Use reflection to remove all old actions
        Field f = mNotificationBuilder.getClass().getDeclaredField("mActions");
        f.setAccessible(true);
        f.set(mNotificationBuilder, new ArrayList<>());
    } 
    catch (NoSuchFieldException e) {} 
    catch (IllegalAccessException e) {}
    
like image 44
prom85 Avatar answered Sep 27 '22 15:09

prom85