我有一个显示开始计时器按钮的通知。在 API25 上,此按钮可以正常工作,但在 API26 及更高版本上,该按钮没有任何作用。
我已经设置了一个通知渠道。通知本身显示正确,因此我不认为是渠道问题。
这里我添加了播放/暂停意图
var playPauseIntent = new Intent("com.example.example.timer");
var timerNotificationIntentValue = this.GetTimerNotificationIntentValue(timerAction);
playPauseIntent.PutExtra("timerNotification", timerNotificationIntentValue);
const int playPauseIntentId = 0;
var playPausePendingIntent = PendingIntent.GetBroadcast(this.context, playPauseIntentId, playPauseIntent, PendingIntentFlags.UpdateCurrent);
contentView.SetOnClickPendingIntent(Resource.Id.btn_start_pause, playPausePendingIntent);
这就是我创建通知的方法:
var channelId = "11e9a3a1-aebf-425d-a9e8-fcc2fb139664";
var channelName = "General";
NotificationChannel channel;
channel = notificationManager.GetNotificationChannel(channelName);
if (channel == null)
{
channel = new NotificationChannel(channelId, channelName, NotificationImportance.Max);
channel.LockscreenVisibility = NotificationVisibility.Public;
notificationManager.CreateNotificationChannel(channel);
}
channel?.Dispose();
Notification.DecoratedCustomViewStyle notificationStyle = new Notification.DecoratedCustomViewStyle();
notification = new Notification.Builder(this.context, "11e9a3a1-aebf-425d-a9e8-fcc2fb139664")
.SetSmallIcon(Resource.Drawable.ic_logo)
.SetStyle(notificationStyle)
.SetCustomContentView(contentView)
.Build();
然后我只需通知一下
notification.Flags |= NotificationFlags.OngoingEvent;
notificationManager.Notify("1", notification);
点击按钮时,在API26上没有任何反应。
我注意到的一些情况是,在API25上,当我点击按钮时,我可以到达广播接收器,而在API26上则不能。
playPauseIntent.SetClass(this,typeof(your broadcastreceiver));
,或者你可以分享你的项目,我会测试它。 - Leo Zhu