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 ...
随机推荐
- 洛谷 P1372 又是毕业季I[数论/神坑规律题]
题目描述 为了把毕业晚会办得更好,老师想要挑出默契程度最大的k个人参与毕业晚会彩排.可是如何挑呢?老师列出全班同学的号数1,2,……,n,并且相信k个人的默契程度便是他们的最大公约数(这不是迷信哦~) ...
- cogs——1008. 贪婪大陆(清华巨佬代码)——树状数组
1008. 贪婪大陆 ★★ 输入文件:greedisland.in 输出文件:greedisland.out 简单对比时间限制:1 s 内存限制:128 MB 试题四:贪婪大陆 [题 ...
- SQLite中特殊的INSERT语句
SQLite中特殊的INSERT语句 在SQLite中,INSERT是基本语句,用来向表中插入数据.但是当表中存在字段存在唯一.非空.检查.主键等约束时,插入的数据很容易和约束冲突,造成插入操作失 ...
- CSS定位与布局:浮动
浮动的特点 浮动(float)属性提出的作用是实现文字的环绕效果,一个元素浮动后,会脱离普通流.主要的特点如下: 浮动的元素会向左或者向右移动直到它的外边缘接触容器框(containing blo ...
- Geoserver跨域问题
1 Geoserver位war包,使用Tomcat发布的情况 由于geoserver为war形式,需要解决tomcat跨域问题,geoserver版本为2.9.1. (1)下载跨域war包 下载cor ...
- 适配iOS7之—UITableView和UISearchBar
iOS7中,如果用UITableViewStyleGrouped的话,里面的cell会比原来的拉长了,这样做应该是为了统一和UITableViewStylePlain风格时cell的大小一致,所以改用 ...
- AIX 删除指定目录、指定后缀、指定天数以前的历史文件
find /home/oracle/admin/zhjport/udump/ -name "*.trc" -mtime +30 -exec rm {} \; 如果要自动执行可以加入 ...
- Form元素示例
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- pandas常用操作
删除某列: concatdfs.drop('Unnamed: 0',axis=1) 打印所有列名: .columns
- JS: document.getElementBy(), setInerval()
ylbtech-JavaScript-DOM document.getElementBy(),setInerval() 1.A,document.getElementBy()返回顶部 document ...