Skip to content Skip to sidebar Skip to footer

Update Notification's Action Icon Dynamically

I set a notification for a player, I want to update icon after play or pause action from notification. This is my code for Notification. private void showNotification(String ti

Solution 1:

You should use

.addAction(getResources.getDrawable(R.drawable.exo_controls_pause),"",playPendingIntent)

Solution 2:

Solution To change the icon of action at realtime, you need:

  1. Get and replace action.
  2. Push new notification.

For example:

notificationBuilder.actions[1] = newNotification.Action(R.drawable.pause, "play", pendingIntent);

NotificationManagerservice= ((NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE));
service.notify(101, notificationBuilder);

or

notificationBuilder.actions[1] = new Notification.Action(R.drawable.pause, "play", pendingIntent);
startForeground(101, notificationBuilder);

Solution 3:

The best solution I've found is basically to remove all actions from builder and add them again, then resubmit the notification.

(code in Kotlin)

funstartNotification()
{
    _builder = NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
        .setContentText("Title")
        .setContentIntent(_contentIntent)
        .setOngoing(true)

    updateNotification(true) // set initial playing state here
}

funupdateNotification(playing: Boolean)
{
    val playPauseResource = if (playing) R.drawable.pause else R.drawable.play

    _builder
        .clearActions()
        .addAction(R.drawable.prev, "Rewind", _rewindPendingIntent)
        .addAction(playPauseResource, "Play/pause", _playPendingIntent)
        .addAction(R.drawable.next, "Next", _forwardPendingIntent)

    with(NotificationManagerCompat.from(_context))
    {
        notify(NOTIFICATION_ID, _builder.build())
    }
}

Post a Comment for "Update Notification's Action Icon Dynamically"