关于Android8.0以上系统通知栏不显示的问题
Android 8.0之前,Google爸爸在通知栏增加了NotificationChannel这个属性,其实就是在通知栏builder的时候需要设置一个这样类型的属性,这是Android8.0的新特性,不加的话,通知栏就没法显示,所以为了适配Android8.0,就需要去判断了版本号了啊!!!
private int PUSH_NOTIFICATION_ID = (0x001);private String PUSH_CHANNEL_ID = "PUSH_NOTIFY_ID";private String PUSH_CHANNEL_NAME = "PUSH_NOTIFY_NAME";
mNotifyManager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O) {
//当手机系统版本号大于8.0的时候就需要设置一个NotificationChannel属性 mBuilder = new NotificationCompat.Builder(context,PUSH_CHANNEL_ID); NotificationChannel mChannel = new NotificationChannel(PUSH_CHANNEL_ID, PUSH_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
mNotifyManager.createNotificationChannel(mChannel);
mBuilder.setContentTitle("自己设置") .setContentText("自己设置") .setSmallIcon(R.mipmap.ic_launcher_origin) .setChannelId(PUSH_CHANNEL_ID)} else { mBuilder = new NotificationCompat.Builder(context); mBuilder.setContentTitle("自己设置") .setContentText("自己设置") .setSmallIcon(R.mipmap.ic_launcher_origin);}notification = mBuilder.build();
在我们notify的时候,即更新通知栏的时候,同样也不能忘了这个PUSH_NOTIFICATION_ID
即:mNotifyManager.notify(PUSH_NOTIFICATION_ID, notification);
之后你就可以看到大功告成了呦
网友评论