user3184899
user3184899

Reputation: 3069

Change Notification's action icon dynamically

I'm trying to create a notification for my app that has an action that I can change its icon, title and intent when I click on it.

I see its possible here at 3:33

https://www.youtube.com/watch?v=tKoQatxG0_8&index=73&list=PLOU2XLYxmsIIwGK7v7jg3gQvIAWJzdat_

But I couldn't find a way to do it.

also, If someone know how to use the pause/play icon on the Right watch in the link, i'll like to know that also.

Thanks from advance.

Upvotes: 7

Views: 3303

Answers (3)

Ravi Patel
Ravi Patel

Reputation: 2191

By following @Sammer J suggestion I came up with the following solution.

try {
    // mBuilder is a Notification.Builder object.
    Field field = mBuilder.getClass().getDeclaredField("mActions");
    field.setAccessible(true);
    ArrayList<Notification.Action> mActions = (ArrayList<Notification.Action>) field.get(mBuilder);
    if (mActions != null) {
        mActions.set(0, new Notification.Action(R.drawable.ic_action_resume, getString(R.string.button_resume), pendingIntent));
        field.set(mBuilder, mActions);
    }
} catch (NoSuchFieldException | IllegalAccessException e) {
    e.printStackTrace();
}
mNotificationManager.notify(ONGOING_NOTIFICATION_ID, mBuilder.build());

Upvotes: 0

E Player
E Player

Reputation: 1856

You can access notification actions from notificationObj.actions

Try below: (Note: below code is not complete, but it will give you an idea on how to change action icon)

Notification status = null;
private NotificationCompat.Builder mBuilder;
private NotificationManager mManager;
private final int STATUS_ID = 1;
private String CHANNEL_ID = "channel_name";

private void setUpNotification() {
     mManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

     int playOrPause;
     if(isPlaying()) {
       playOrPause = R.drawable.ic_pause;
     } else {
       playOrPause = R.drawable.ic_play;
     }

     if(mBuilder == null) {
          //Setup Builder
          mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);
          mBuilder.setOngoing(true) 
                    .addAction(playOrPause, "play", pendingIntent1)      // Index 0
                    .addAction(R.drawable.ic_2, "text2", pendingIntent2) // Index 1
                    .addAction(R.drawable.ic_3, "text3", pendingIntent3);// Index 2

         status = mBuilder.build(); 
     } else {
         //Update builder as per your needs

         mBuilder.setContentTitle(title);
         status = mBuilder.build();
         status.actions[0] = new Notification.Action(playOrPause, "play", pendingIntent1);

     }

     startForeground(STATUS_ID, status);

}

Upvotes: 3

Sameer J
Sameer J

Reputation: 226

I was able to solve the same problem by accessing builder.mActions. It's an ArrayList of all the actions you've added. I modify this without recreating the builder and calling build seems to update this.

This doesn't seem to be documented in the SDK but I'm ok with that for now.

Will let you know if I come up with something else

Upvotes: 1

Related Questions