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());
        //代码省略
复制代码

完成以上适配基本就没问题了,如果使用了第三方推送的同学,请去更新最新SDK。

 

参考链接:解决Fail to post notification on channel "null"的方法

 

 


如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的“推荐”将是我最大的写作动力!欢迎各位转载,但是未经作者本人同意,转载文章之后必须在文章页面明显位置给出作者和原文连接,否则保留追究法律责任的权利。
posted @   夜行过客  阅读(631)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
历史上的今天:
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)-拷贝构造函数
点击右上角即可分享
微信分享提示