关于Android无法弹出Notification 的解决方案
注释:Android8.0以下版本按正常四步骤即可设置消息弹窗,而Android8.0以上版本则需要添加一个channel_id,方式如下:
注释:图三代码如下:
//全局变量:
String channel_id="myChannelId";
String channel_name="myChannelName";
String description = "this is myChannel's description";
private void createNotificationChannel(){
//Android8.0(API26)以上需要调用下列方法,但低版本由于支持库旧,不支持调用
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel(channel_id,channel_name,importance);
channel.setDescription(description);
notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}else{
notificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);
}
}
注释:Android8.0以下版本会自动跳过createNotificationChannel();