1. 程式人生 > >Notification Android8.0中無法傳送通知,提示:No Channel found for pkg

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

用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