Android-解决Fail to post notification on channel "null"的方法
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
所以如果还用上面的代码就会跳出这个错误
传送门:https://www.ithome.com/html/android/298943.htm
代码如下
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
不过个人建议是加一个版本判断 因为可以加上另外一段代码来兼容25之前的平台
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
{
//当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"的方法的更多相关文章
- 解决Fail to post notification on channel "null"的方法
mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);mNotifyMgr.cancelAll(); St ...
- Android:解决client从server上获取数据乱码的方法
向server发送HTTP请求.接收到的JSON包为response,用String content = EntityUtils.toString(response.getEntity()," ...
- android解决内存溢出的问题(没有从根本上解决)
Android游戏虚拟机算法JNI 尽量不要使用setImageBitmap或setImageResource或BitmapFactory.decodeResource来设置一张大图,因为这些函数在完 ...
- [Android]解决3gwap联网失败:联网请求在设置代理与直连两种方式的切换
[Android]解决3gwap联网失败:联网请求在设置代理与直连两种方式的切换 问题现象: 碰到一个问题,UI交互表现为:联通号码在3gwap网络环境下资源一直无法下载成功. 查看Log日志,打印出 ...
- Android开发学习之路--Notification之初体验
一般当我们收到短信啊,微信啊,或者有些app的提醒,我们都会在通知栏收到一天简单的消息,然后点击消息进入到app里面,其实android中有专门的Notification的类可以完成这个工作,这里就实 ...
- Android解决自定义View获取不到焦点的情况
引言: 我们在使用Android View或者SurfaceView进行图形绘制,可以绘制各种各样我们喜欢的图形,然后满怀信心的给我们的View加上onTouchEvent.onKeyDown.onK ...
- Android解决Intent中的数据重复问题
转载地址:http://www.cnblogs.com/anrainie/articles/2383941.html 最近在研究Android,遇到了一些Notification(通知)的问题: .N ...
- Android 解决qq分享后返回程序出现的Bug
问题:当我们使用qq分享时,分享成功后选择留在qq,这个时候按home键,回到手机主界面,在点击回到我的app,这个时候会出现界面显示出来了,但是任何事件都不响应,即按钮没反应. 分析:这个时候回到我 ...
- Android 解决ScrollView嵌套RecyclerView导致滑动不流畅的问题
最近做的项目中遇到了ScrollView嵌套RecyclerView,刚写完功能测试,直接卡出翔了,后来通过网上查找资料和 自己的实践,找出了两种方法解决这个问题. 首先来个最简单的方法: recyc ...
随机推荐
- fastboot flash image usage
// // power on mobile phone // power on mobile phone // // reboot bootloader // android/out/host/lin ...
- spingboot 邮件模板发送;
<!-- 邮件start --><dependency> <groupId>javax.mail</groupId> <artifactId> ...
- POJ1655 Balancing Act(树的重心)
题目链接 Balancing Act 就是求一棵树的重心,然后统计答案. #include <bits/stdc++.h> using namespace std; #define REP ...
- Codeforces 371B Fox Dividing Cheese(简单数论)
题目链接 Fox Dividing Cheese 思路:求出两个数a和b的最大公约数g,然后求出a/g,b/g,分别记为c和d. 然后考虑c和d,若c或d中存在不为2,3,5的质因子,则直接输出-1( ...
- API网关服务Zuul-Spring Cloud学习第五天(非原创)
文章大纲 一.Zuul是什么二.Zuul的基本实现三.路由配置细节四.异常处理细节五.项目源码与参考资料下载六.参考文章 一.Zuul是什么 到目前为止,我们Spring Cloud中的内容已 ...
- laravel 性能提升
php artisan optimize 相当于: 1.composer dump-autoload --optimize // composer 层面优化加载速度 2.php artisan cle ...
- 聊聊、Zookeeper Linux 启动
Zookeeper 在 windows 下安装比较简单,属于无脑式安装,下载下来双击脚本就可以了.前面的文章中也有介绍,今天我来写写 Linux 下的安装,以及所碰到的坑. 首先,登陆 Linux 系 ...
- mac 配置pylucene
1 . 安装python 2 安装ant 3 下载pylucene 4 cd jcc , vim setup.py , 查看java路径和python路径是否正确 5 python setup. ...
- HDU 3150 Robot Roll Call – Cambot…Servo…Gypsy…Croooow(map)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3150 Problem Description Mystery Science Theater 3000 ...
- 线程中的WaitForSingleObject和Event的用法
http://chinaxyw.iteye.com/blog/548622 首先介绍CreateEvent是创建windows事件的意思,作用主要用在判断线程退出,程锁定方面. CreateEvent ...