android: 安卓8.0系统notification适配Failed to post notification on channel “null”(转)
本文转自:https://blog.csdn.net/HUandroid/article/details/78972228
刚刚升级了8.0系统后,模拟器突然出现以下错误“Failed to post notification on channel “null””
由于8.0通知栏增加了channel渠道式消息分发机制,在Android文档中编写的:
https://developer.android.com/preview/features/notification-channels.html
所以要去对8.0系统进行适配:
//代码省略 String name = "my_package_channel";//渠道名字 String id = "my_package_channel_1"; // 渠道ID String description = "my_package_first_channel"; // 渠道解释说明 PendingIntent pendingIntent;//非紧急意图,可设置可不设置 if (notificationManager == null) { notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); } //判断是否是8.0上设备 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { int importance = NotificationManager.IMPORTANCE_LOW; NotificationChannel mChannel = null; if (mChannel == null) { mChannel = new NotificationChannel(id, name, importance); mChannel.setDescription(description); mChannel.enableLights(true); //是否在桌面icon右上角展示小红点 notificationManager.createNotificationChannel(mChannel); } notificationBuilder = new NotificationCompat.Builder(this); intent = new Intent(this, DownloadService.class); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); pendingIntent = PendingIntent.getActivity(this, 0, intent, 0); notificationBuilder. setSmallIcon(R.mipmap.logo) .setContentTitle(app_name) .setContentText(app_name+"正在下载......") //.setContentIntent(pendingIntent) .setChannelId(id) .setAutoCancel(true); }else{ notificationBuilder = new NotificationCompat.Builder(this) .setSmallIcon(R.mipmap.logo) .setContentTitle(app_name) .setContentText(app_name+"正在下载......") .setAutoCancel(true); } notificationManager.notify(0, notificationBuilder.build()); //代码省略
如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
2018-07-06 android framework-下载Android系统源代码
2017-07-06 C++语言基础(25)-C++格式化输出
2017-07-06 C++语言基础(24)-四种类型转换运算符(static_cast、dynamic_cast、const_cast和reinterpret_cast)
2017-07-06 C++语言基础(23)-拷贝构造函数