动态更新通知的操作图标

5

我为一个播放器设置了通知,我希望在通知的播放或暂停操作之后更新图标。 以下是我的通知代码:

    private void showNotification(String title, Bitmap bitmap) {

    //region Create Notification
    MediaSessionCompat mediaSession = new MediaSessionCompat(getApplicationContext(), "session tag");
    MediaSessionCompat.Token token = mediaSession.getSessionToken();
    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this,"PRIMARY_CHANNEL")
             .setSmallIcon(R.mipmap.ic_launcher_1)
             .setLargeIcon(bitmap)
             .setContentTitle("Simplay")
             .setContentText(title)
             .setOngoing(true)
             .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setContentIntent(notifyPendingIntent)
             .addAction(R.drawable.exo_controls_rewind,"",rewindPendingIntent)
             **.addAction( R.drawable.exo_controls_pause,"",playPendingIntent)**
             .addAction(R.drawable.exo_controls_fastforward,"",forwardPendingIntent)
             .setStyle(new android.support.v4.media.app.NotificationCompat.MediaStyle()
             .setMediaSession(token))
             .setPriority(NotificationCompat.PRIORITY_HIGH);

    notificationManager = NotificationManagerCompat.from(this);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "Notification";
        String description = "";
        int importance = NotificationManager.IMPORTANCE_HIGH;
        NotificationChannel channel = new NotificationChannel("PRIMARY_CHANNEL", name, importance);
        channel.setDescription(description);
        channel.enableLights(true);
        channel.setShowBadge(false);
        channel.setLockscreenVisibility(123);
        NotificationManager notificationManager1 = getSystemService(NotificationManager.class);
        notificationManager1.createNotificationChannel(channel);
    }
    notificationManager.notify(123, mBuilder.build());
    //endregion
}
3个回答

0
我找到的最佳解决方案基本上是从构建器中删除所有操作,然后再添加它们,然后重新提交通知。
(Kotlin代码)
fun startNotification()
{
    _builder = NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID)
        .setContentText("Title")
        .setContentIntent(_contentIntent)
        .setOngoing(true)

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

fun updateNotification(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())
    }
}

0

你应该使用

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

1
这将添加一个新的操作,而不是更新旧的操作。 - Luis A. Florit

0
解决方案 要实时更改操作图标,您需要执行以下步骤:
  1. 获取并替换操作。
  2. 推送新通知。

例如:

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

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

或者

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接