android - Android:通知不会显示
问题描述
我正在我的应用程序中开发一个简单的服务,该服务还发送通知以向用户显示该服务正在运行。这确实有效,但从一天到另一天都停止了。我知道代码被正确调用了。我在这些行中设置了一个测试吐司,每半秒调用一次,所以它应该可以工作,但通知栏中没有显示通知。请看一看:
void DispatchNotificationThatServiceIsRunning()
{
_notificationIsLive = true;
RemoteViews contentView = new RemoteViews(PackageName, Resource.Layout.Notification);
contentView.SetTextViewText(Resource.Id.txt_crrentSong_notification, Activity_Player.txt_CurrentSong.Text);
contentView.SetTextViewText(Resource.Id.txt_crrentSong__artist_notification, Activity_Player.txt_CurrentArtist.Text);
notificationBuilder = new Notification.Builder(this);
Task.Delay(_countDownFirstRound).ContinueWith(t =>
{
notificationBuilder
.SetSmallIcon(Resource.Drawable.btn_icon_header)
.SetCustomContentView(contentView);
var notificationManager = (NotificationManager)GetSystemService(NotificationService);
notificationManager.Notify(50, notificationBuilder.Build());
DispatchNotificationThatServiceIsRunning();//This is for repeate every 1s.
_countDownFirstRound = 50;
// Click on notification, and return to app. Only works, because _2TimerRunning is set as : "LaunchMode = LaunchMode.SingleInstance"
Intent notificationIntent = new Intent(this, typeof(Activity_Player));
notificationIntent.SetAction(Intent.ActionMain);
notificationIntent.AddCategory(Intent.CategoryLauncher);
PendingIntent contentIntent = PendingIntent.GetActivity(this, 1, notificationIntent, PendingIntentFlags.UpdateCurrent);
notificationBuilder.SetContentIntent(contentIntent);
},
TaskScheduler.FromCurrentSynchronizationContext());
}
}
解决方案
从 Android 8.0(API 级别 26)开始,所有通知都必须分配给一个频道。请参阅此链接..创建和管理通知渠道
在 notificationManager.Notify(50, notificationBuilder.Build()); 之前添加此代码
String channelId = "Default";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, "Default channel", NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(channel);
}
推荐阅读
- yaml - 机器人框架在 for 循环中使用 yaml 列表
- python - 如何将值添加到嵌套字典?
- android - 在 Android Webview 中临时增加屏幕亮度
- c++ - 查找存在于第一个数组中而不存在于第二个数组中的元素
- c++ - 将泛型对象传递给 Rust 并在使用后传回 C++ 进行销毁
- java - 从文本文件中解析一行并访问括号内的值
- php - 将laravel默认注册重定向到以前的url或页面
- reactjs - 在 NextJS 中,我们收到错误“React Component as default export in”,但它是针对我想要在目录中的文件
- c++ - 无法为 X.radio 创建中等可视化,因为未找到网络节点可视化 - Omnet++
- javascript - 将php数组变量传递给javascript以获取传单地图