gpt4 book ai didi

android - Notification.addAction 在 Android O 中不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:48:04 25 4
gpt4 key购买 nike

下面提到的代码适用于低于 android O verison 的所有设备。对于安卓 O,addAction() 方法不起作用,即按钮点击在 android O 中不起作用。
任何帮助将不胜感激。

 NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

Intent mediaPlayerReceiver = new Intent("com.consult.news.receiver.ACTION_PLAY");
mediaPlayerReceiver.putExtra("NewsArticle", news);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, mediaPlayerReceiver, PendingIntent.FLAG_UPDATE_CURRENT);

Intent dismissNotification = new Intent("com.consult.news.receiver.DISMISS");
dismissNotification.putExtra("Notification_ID", 1);
PendingIntent dismissNotificationIntent = PendingIntent.getBroadcast(context, 0, dismissNotification, PendingIntent.FLAG_UPDATE_CURRENT);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
String CHANNEL_ID = "my_channel_01";
String CHANNEL_NAME = "my Channel Name";

NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
notificationChannel.enableLights(true);
notificationChannel.setLightColor(Color.RED);
notificationChannel.setShowBadge(true);
notificationChannel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);
notificationManager.createNotificationChannel(notificationChannel);
}

NotificationCompat.Builder builder = new NotificationCompat.Builder(context, "my_channel_01")
.setSmallIcon(R.drawable.ic_notification_white)
.setColor(ContextCompat.getColor(context, R.color.accent))
.setContentTitle(context.getString(R.string.Consult_Univadis_Title))
.setStyle(new NotificationCompat.BigTextStyle().bigText(news))
.addAction(isPlaying ? R.drawable.ic_notification_white : R.drawable.ic_notification_white, isPlaying ? "Play" : "Pause", pendingIntent)
.addAction(R.drawable.ic_notification_white, "Close", dismissNotificationIntent)
.setOngoing(true)
.setAutoCancel(false);

notificationManager.notify(1, builder.build());

最佳答案

我遇到了同样的情况,在 Android Oreo 中你需要让它成为一个明确的 Intent(将接收器放在 list 上是不够的,事实上,它不会注意这一点),所以当你制作 Intent ,使用 setClass 方法使其明确:

Intent mediaPlayerReceiver = new Intent("com.consult.news.receiver.ACTION_PLAY");
mediaPlayerReceiver.putExtra("NewsArticle", news);
mediaPlayerReceiver.setClass(this, YourReceiver.class);

“this”是 Context,YourReceiver 是您希望收听操作的 Receiver 类。

您必须对 dismissNotification Intent 执行相同的操作

如果这对您有用,请告诉我。

关于android - Notification.addAction 在 Android O 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47032185/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com