Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 765|回复: 0

Android-解决Fail to post notification on channel "null"的方法

[复制链接]
  • TA的每日心情
    奋斗
    2024-4-6 11:05
  • 签到天数: 748 天

    [LV.9]以坛为家II

    2034

    主题

    2092

    帖子

    70万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    705612
    发表于 2021-5-20 23:50:47 | 显示全部楼层 |阅读模式
    原文:https://blog.csdn.net/weixin_40604111/article/details/78674563
    在sdk版本为25或25之前想在notification中添加一个点击事件 只要通过setContentIntent()传入一个PendingIntent就可以实现通知点击事件 代码如下
     
    Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.baidu.com"));
    PendingIntentpendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
    NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    Notification notification = new NotificationCompat.Builder(MainActivity.this)
                                    .setContentTitle("This is content title")
                                    .setContentText("This is content text")
                                    .setSmallIcon(R.mipmap.ic_launcher)
                                    .build();
    manager.notify(1,notification);123456789
    但对于不少像我一样的新手用的模拟器或者调试工具都是最新版本即sdk为26的平台
    所以如果还用上面的代码就会跳出这个错误

    当时最后是在一个Android O的更新说明中找到了答案
    传送门:https://www.ithome.com/html/android/298943.htm
     
    再反观错误提示
    Failed to post notification on channel “null”
    这个时候我们就知道问题是什么啦
    意思就是在Android O后 引入了一个叫NotificationChannel的类 在sdk版本为26的时候 我们不加这个东西 就设置用不了点击事件啦
    就我个人的理解 NotificationChannel的作用就是细化对notification的设置 之前关于notification的设置都是可以在Notification.Builder(Context,int)中完成
    引入NotificationChannel后  关于震动 声音 提示灯 优先级的设置就可以在NotificationChannel中设置
    不过个人测试后 感觉Android O在通知方面更注重用户了 就算你在代码中设置了重要性 但是实际提示的效果还是根据用户在手机中设置的通知重要性来判断 所以个人感觉开发者在代码设置重要性这部分可以直接略去
    加入NotificationChannel后
    代码如下

    String id ="channel_1";//channel的id
    String description = "123";//channel的描述信息
    int importance = NotificationManager.IMPORTANCE_LOW;//channel的重要性
    NotificationChannel channel = new NotificationChannel(id, "123", importance);//生成channel
    //为channel添加属性
    //channel.enableVibration(true); 震动
    //channel.enableLights(true);提示灯
    NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    manager.createNotificationChannel(channel);//添加channel
    Notification notification = new Notification.Builder(MainActivity.this,id)
                                        //注意这里多了一个参数id,指配置的NotificationChannel的id
                                        //你可以自己去试一下 运行一次后 即配置完后 将这行代码以上的代
                                        //码注释掉 将参数id直接改成“channel_1”也可以成功运行
                                        //但改成别的如“channel_2”就不行了
                                        .setCategory(Notification.CATEGORY_MESSAGE)
                                        .setSmallIcon(R.mipmap.ic_launcher)
                                        .setContentTitle("This is a content title")
                                        .setContentText("This is a content text")
                                        .setContentIntent(pendingIntent)
                                        .setAutoCancel(true)
                                        .build();
    manager.notify(1,notification);1234567891011121314151617181920212223
    不过要用于项目中 还是不行 因为我们要考虑一个兼容版本问题 所以还要加上一个版本判断 或者 是一个requireApi为Android O 
    不过个人建议是加一个版本判断 因为可以加上另外一段代码来兼容25之前的平台
    下面是最终代码
     
    Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("http://www.baidu.com"));
    PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
    NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
     if(Build.VERSION.SDK_INT >= 26)
     {
                   //当sdk版本大于26
       String id = "channel_1";
       String description = "143";
       int importance = NotificationManager.IMPORTANCE_LOW;
       NotificationChannel channel = new NotificationChannel(id, description, importance);
    //                     channel.enableLights(true);
    //                     channel.enableVibration(true);//
       manager.createNotificationChannel(channel);
       Notification notification = new Notification.Builder(MainActivity.this, id)
                                        .setCategory(Notification.CATEGORY_MESSAGE)
                                        .setSmallIcon(R.mipmap.ic_launcher)
                                        .setContentTitle("This is a content title")
                                        .setContentText("This is a content text")
                                        .setContentIntent(pendingIntent)
                                        .setAutoCancel(true)
                                        .build();
       manager.notify(1, notification);
       }
       else
       {
                //当sdk版本小于26
        Notification notification = new NotificationCompat.Builder(MainActivity.this)
                                        .setContentTitle("This is content title")
                                        .setContentText("This is content text")
                                        .setContentIntent(pendingIntent)
                                        .setSmallIcon(R.mipmap.ic_launcher)
                                        .build();
        manager.notify(1,notification);
       }
    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-5-19 14:59 , Processed in 0.070336 second(s), 30 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表