Notification Android8.0中无法发送通知,提示:No Channel found for pkg

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010356768/article/details/83546008

用Android 8.0的手机进行发送通知的测试,发现通知不能在系统状态栏显示出来,查看Logcat,发现warning如下
No Channel found for pkg=com.example.xx.xx, channelId=null, id=1001, tag=null…

原来是由于此条通知没有查找到应用中对应的NotificationChannel的原因,而无法弹出来,查阅资料得知,NotificationChannel是Android O新增的通知渠道,其允许您为要显示的每种通知类型创建用户可自定义的渠道

如果你需要发送属于某个自定义渠道的通知,你需要在发送通知前创建自定义通知渠道,示例如下:

//ChannelId为"001",ChannelName为"my_channel"
NotificationChannel channel = new NotificationChannel("1",
                "my_channel", NotificationManager.IMPORTANCE_DEFAULT);
channel.enableLights(true); //是否在桌面icon右上角展示小红点
channel.setLightColor(Color.GREEN); //小红点颜色
channel.setShowBadge(true); //是否在久按桌面图标时显示此渠道的通知
notificationManager.createNotificationChannel(channel);

//同时,Notification.Builder需要多设置一个
builder.setChannelId("001");

一个完整的发送通知的栗子可以看
https://blog.csdn.net/u010356768/article/details/83382446

猜你喜欢

转载自blog.csdn.net/u010356768/article/details/83546008