Android 8.0 notification 不显示的bug

8.0之后的android 需要创建 channel 否则 notification 不显示。

final String CHANNEL_ID = "channel_id_1";
final String CHANNEL_NAME = "channel_name_1";

NotificationManager mNotificationManager = (NotificationManager)
getSystemService(Context.NOTIFICATION_SERVICE);

if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
//只在Android O之上需要渠道
NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID,
CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
//如果这里用IMPORTANCE_NOENE就需要在系统的设置里面开启渠道,
//通知才能正常弹出
mNotificationManager.createNotificationChannel(notificationChannel);
}
NotificationCompat.Builder builder= new NotificationCompat.Builder(this,CHANNEL_ID);

builder.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle("通知标题")
.setContentText("通知内容")
.setAutoCancel(true);

mNotificationManager.notify(notificationId, builder.build());

发表评论

电子邮件地址不会被公开。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据