Xamarin.Android自定义控件的通知在API26中无法工作

4

我有一个显示开始计时器按钮的通知。在 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上则不能。


它在我的Android 8.0上使用你的代码可以工作,我不知道为什么在你那边不能工作,也许你可以尝试添加playPauseIntent.SetClass(this,typeof(your broadcastreceiver));,或者你可以分享你的项目,我会测试它。 - Leo Zhu
抱歉,我不能分享项目代码。 - Santiago Quiroga
它能与 setClass 一起使用吗? - Leo Zhu
不,它没有。我不明白问题在哪里... 代码似乎没问题。 - Santiago Quiroga
是的,我使用您上面的代码进行了测试,它可以正常工作。 - Leo Zhu
显示剩余5条评论
1个回答

0

修改了这个

var playPauseIntent = new Intent("com.example.example.timer");

var playPauseIntent = new Intent(this.context, typeof(MyNotificationBroadcastReceiver));

现在看起来它似乎正常工作。


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