首页 > 解决方案 > 带有自定义控件的 Xamarin.Android 通知在 API26 中不起作用

问题描述

我有一个通知,显示一个按钮来启动计时器。此按钮在 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 上,当我点击按钮时,我到达了 BroadcastReceiver,而在 API26 上我没有

标签: c#xamarin.androidandroid-notifications

解决方案


改变了这个

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

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

现在它似乎工作正常。


推荐阅读