解决Fail to post notification on channel "null"的方法
来源:互联网 发布:设a是2n 1阶矩阵 编辑:程序博客网 时间:2024/06/18 06:43
新手 网上关于这个问题的解决方法有点少 下面是自己的一些理解和测试后确实可用的代码
在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);
但对于不少像我一样的新手用的模拟器或者调试工具都是最新版本即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的idString 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);//添加channelNotification 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);
不过要用于项目中 还是不行 因为我们要考虑一个兼容版本问题 所以还要加上一个版本判断 或者 是一个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); }
- 解决Fail to post notification on channel "null"的方法
- Failed to post notification on channel "null"消息栏无法显示
- 安卓通知栏报错 :Failed to post notification on channel "null"
- Install PG fail on windows: Problem running post-install step.
- Genymotion模拟器出现Unable to create virtual device Fail to create SD card错误的解决
- php进行递归时出现Call to a member function 方法() on null的原因以及解决方案
- CTS: testSensorFeatures FAIL 的解决
- 我的电脑的ADT软件解决Fail to create the java Virtual Machine问题
- VMWARE8 开启文件共享。xp与ubuntu10.04. fail to configure wmware workstation server 的解决
- Eclipse启动时fail to create Java Virtual Machine问题的解决
- Protel DXP出现错误" Fail to load Parallel Port Driver" 的解决
- Eclipse启动时fail to create Java Virtual Machine问题的解决
- Eclipse启动时fail to create Java Virtual Machine问题的解决
- Eclipse启动时提示fail to create the Java Virtual Machine问题的解决
- Eclipse启动时fail to create Java Virtual Machine问题的解决
- Eclipse启动时fail to create Java Virtual Machine问题的解决
- Eclipse启动时fail to create Java Virtual Machine问题的解决
- 解决eclipse载入已有maven工程时出现的fail to read artifact descriptor....问题
- Long Short-Term Memory(LSTM)
- 【Maven问题】应用Maven构建项目遇到的各种问题总结
- chrome64新增的Performance Monitor
- 物联网平台集成实践分享
- Spring自定义xml标签
- 解决Fail to post notification on channel "null"的方法
- 前端面试题(2017-11-30)
- C/C++资料网址
- json数据
- HttpClient get请求调取接口 例子
- SQL Server数据库学习之 -- 存储过程-游标-表值类型综合运用
- 无滚动条切屏
- 检查型异常和运行时异常
- gulp task方法