原文: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);
   }

Android-解决Fail to post notification on channel "null"的方法的更多相关文章

  1. 解决Fail to post notification on channel "null"的方法

    mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);mNotifyMgr.cancelAll(); St ...

  2. Android:解决client从server上获取数据乱码的方法

    向server发送HTTP请求.接收到的JSON包为response,用String content = EntityUtils.toString(response.getEntity()," ...

  3. android解决内存溢出的问题(没有从根本上解决)

    Android游戏虚拟机算法JNI 尽量不要使用setImageBitmap或setImageResource或BitmapFactory.decodeResource来设置一张大图,因为这些函数在完 ...

  4. [Android]解决3gwap联网失败:联网请求在设置代理与直连两种方式的切换

    [Android]解决3gwap联网失败:联网请求在设置代理与直连两种方式的切换 问题现象: 碰到一个问题,UI交互表现为:联通号码在3gwap网络环境下资源一直无法下载成功. 查看Log日志,打印出 ...

  5. Android开发学习之路--Notification之初体验

    一般当我们收到短信啊,微信啊,或者有些app的提醒,我们都会在通知栏收到一天简单的消息,然后点击消息进入到app里面,其实android中有专门的Notification的类可以完成这个工作,这里就实 ...

  6. Android解决自定义View获取不到焦点的情况

    引言: 我们在使用Android View或者SurfaceView进行图形绘制,可以绘制各种各样我们喜欢的图形,然后满怀信心的给我们的View加上onTouchEvent.onKeyDown.onK ...

  7. Android解决Intent中的数据重复问题

    转载地址:http://www.cnblogs.com/anrainie/articles/2383941.html 最近在研究Android,遇到了一些Notification(通知)的问题: .N ...

  8. Android 解决qq分享后返回程序出现的Bug

    问题:当我们使用qq分享时,分享成功后选择留在qq,这个时候按home键,回到手机主界面,在点击回到我的app,这个时候会出现界面显示出来了,但是任何事件都不响应,即按钮没反应. 分析:这个时候回到我 ...

  9. Android 解决ScrollView嵌套RecyclerView导致滑动不流畅的问题

    最近做的项目中遇到了ScrollView嵌套RecyclerView,刚写完功能测试,直接卡出翔了,后来通过网上查找资料和 自己的实践,找出了两种方法解决这个问题. 首先来个最简单的方法: recyc ...

随机推荐

  1. hdu 4519(数学题)

    郑厂长系列故事——体检 Time Limit: 500/200 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total S ...

  2. autofac 用法总结

    autofac官网: http://autofaccn.readthedocs.io/en/latest/getting-started/index.html autofac作为一个热门ioc框架,还 ...

  3. win7下用PyInstaller把Python代码打包成exe文件

    2013-11-05 22:02:14|   1.安装 使用PyInstaller需要安装PyWin32. 下载与Python对应的PyInstaller版本,解压后就算安装好了. 例如,安装了PyI ...

  4. ansible 手册

    ansible 官方文档:https://docs.ansible.com/ansible/latest/index.html ansible 中文入门:http://getansible.com/ ...

  5. .NET Core微服务 权限系统+工作流(一)权限系统

    一.前言 实际上权限系统老早之前我就在一直开发,大概在刚毕业没多久就想一个人写一个系统,断断续续一直坚持到现在,毕竟自己亲动手自写的系统才有收获,本篇仅介绍权限. 小小系统上不了台面,望各位大神勿喷. ...

  6. Wordpress笔记:背景音乐

    想要在blog首页加上自动播放的背景音乐,能找到的插件与方法基本上都是讲在文章里添加的.侧边栏里添加文本的方法也总有美观等等这样那样的问题,折腾了几种之后终于搞定了.用的是Audio player插件 ...

  7. Spring Cloud Stream介绍-Spring Cloud学习第八天(非原创)

    文章大纲 一.什么是Spring Cloud Stream二.Spring Cloud Stream使用介绍三.Spring Cloud Stream使用细节四.参考文章 一.什么是Spring Cl ...

  8. Java---Static内存图详解

    案例: 输出结果 内存图: 执行流程: java文件通过编译成class文件,class文件通过类加载器加载到方法区中,程序首先会加载核心类库,也就是你的程序想要运行所需要的一些最基本的类.接着程序会 ...

  9. 2016北京集训测试赛(十四)Problem A: 股神小L

    Solution 考虑怎么卖最赚钱: 肯定是只卖不买啊(笑) 虽然说上面的想法很扯淡, 但它确实能给我们提供一种思路, 我们能不买就不买; 要买的时候就买最便宜的. 我们用一个优先队列来维护股票的价格 ...

  10. 安卓获取软硬件信息并上传给server(Socket实现)

    首先,项目结构如图--A:分为client部分CheckInfo和server端CheckInfo_Server.CheckInfo获取手机信息(Mac,Cpu,内存,已安装软件信息等)并上传到ser ...