c# - 带有自定义控件的 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 上我没有
解决方案
改变了这个
var playPauseIntent = new Intent("com.example.example.timer");
至
var playPauseIntent = new Intent(this.context, typeof(MyNotificationBroadcastReceiver));
现在它似乎工作正常。
推荐阅读
- javascript - 从命令行运行赛普拉斯测试失败,但如果从赛普拉斯测试运行程序运行则通过
- ios - 如何从 Geo JSON 文件中找到纬度和经度?
- hugo - .md 文件的 go 模板变量是什么?
- redux - Redux 可以 reducer 接受多个动作
- java - 如何转换单声道
- > 流式传输
? - c++ - 如何找到此交换函数(单链表)中的错误?
- python-3.x - 在机器学习中,编码非层次分类特征的最佳方法是什么?
- android - 无法将图像从 android 应用程序上传到球衣服务器
- webpack - 如何使用 webpack 忽略或替换一些没有真正使用的模块?
- python - 如何使用 pandas 将字符串与数据框中的字符串进行比较?