原文: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. 最简单方法远程调试Python多进程子程序

    Python 2.6新增的multiprocessing,即多进程,给子进程代码调试有点困难,比如python自带的pdb如果直接在子进程代码里面启动会抛出一堆异常,原因是子进程的stdin/out/ ...

  2. 设置div自适应高度滚动

    <body> <div id="divc" style="overflow: auto;"> </div> <a id ...

  3. ORACLE的字符串操作函数

    字符函数——返回字符值 这些函数全都接收的是字符族类型的参数(CHR 除外)并且返回字符值.除了特别说明的之外,这些函数大部分返回VARCHAR2类型的数值.字符函数的返回类型所受的限制和基本数据库类 ...

  4. 洛谷 P1056 排座椅【贪心/结构体排序】

    题目描述 上课的时候总会有一些同学和前后左右的人交头接耳,这是令小学班主任十分头疼的一件事情.不过,班主任小雪发现了一些有趣的现象,当同学们的座次确定下来之后,只有有限的D对同学上课时会交头接耳.同学 ...

  5. 洛谷——1508 Likecloud-吃、吃、吃

    题目背景 问世间,青春期为何物? 答曰:“甲亢,甲亢,再甲亢:挨饿,挨饿,再挨饿!” 题目描述 正处在某一特定时期之中的李大水牛由于消化系统比较发达,最近一直处在饥饿的状态中.某日上课,正当他饿得头昏 ...

  6. opentracing学习入门

    http://blog.csdn.net/akfly/article/details/53975388

  7. advanced-performance-troubleshooting-waits-latches-spinlocks

    https://www.sqlskills.com/blogs/paul/advanced-performance-troubleshooting-waits-latches-spinlocks/

  8. Spring事务管理——事务的传播行为

    1.简介 当事务方法被另一个事务方法调用时,必须指定事务应该如何传播.例如:方法可能继续在现有事务中运行,也可能开启一个新事务,并在自己的事务中运行. 事务的传播行为可以由传播属性指定.Spring定 ...

  9. An error occurred uploading to the iTunes Store - Please upgrade Java

    Yesterday there were an update to Jave (1.6.0_31) in the "Software update", but now when I ...

  10. Android Touch事件传递机制详解 下

    尊重原创:http://blog.csdn.net/yuanzeyao/article/details/38025165 资源下载:http://download.csdn.net/detail/yu ...