Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Notification's action icon dynamically

Tags:

android

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.

like image 595
user3184899 Avatar asked Jun 28 '14 09:06

user3184899


2 Answers

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

}
like image 191
E Player Plus Avatar answered Oct 18 '22 14:10

E Player Plus


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

like image 24
Sameer J Avatar answered Oct 18 '22 14:10

Sameer J