版权声明:本文转至CSDN博主「dandelionela」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/dandelionela/article/details/86092293
(本文转至dandelionela主页的https://blog.csdn.net/dandelionela/article/details/86092293)
在一个下载任务中(服务+前台通知+异步消息AsyncTask+okhttp),遇到了如下的bug(是前台通知在Android8.0后的bug)(项目链接:https://download.csdn.net/download/ljw874362735/11748409)
后来上网搜了搜,发现是Android8.0对通知做了细分,引进了channel,这主要影响两个方面:
A。普通通知Notification;B。前台服务通知
A.普通通知Notification:
主要相对于传统的写法修改两处,步骤如下:
①在“NotificationManager”中设置添加“NotificationChannel”属性:
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
// 【适配Android8.0】给NotificationManager对象设置NotificationChannel
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel("notification_id", "notification_name", NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(channel);
}
②在“Notification”中使用“setChannelId()”方法设置添加“channel_id”属性:
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
...
...
// 【适配Android8.0】设置Notification的Channel_ID,否则不能正常显示
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId("notification_id");
}
...
builder.build();
其中,第①步骤中的“NotificationChannel”的构造函数中参数列表依次为:
【channel_id】唯一id,String类型;
【channel_name】对用户可见的channel名称,String类型;
【importance_level】通知的重要程度。
importance_level主要有七种层次:
IMPORTANCE_NONE: (0)关闭通知。在任何地方都不会显示,被阻塞
IMPORTANCE_MIN: (1)开启通知。不弹出,不发提示音,状态栏中无显示
IMPORTANCE_LOW: (2)开启通知。不弹出,不发提示音,状态栏中显示
IMPORTANCE_DEFAULT:(3)开启通知。不弹出,发出提示音,状态栏中显示【默认】
IMPORTANCE_HIGH: (4)开启通知。会弹出,发出提示音,状态栏中显示
IMPORTANCE_MAX: (5)开启通知。会弹出,发出提示音,可以使用full screen intents(比如来电)。重要程度最高
IMPORTANCE_UNSPECIFIED:(-1000)表示用户未设重要值。该值是为了持久的首选项,且永不应该与实际通知相关联
B.前台服务通知:
前台服务可通过【startForeground()】方法在通知栏列出前台服务的通知,在传统的写法中类似于普通通知Notification的写法,但是不需要使用NotificationManager来管理显示通知。但是在Android8.0之后引入NotificationChannel之后,即使找不到NotificationManager的使用位置,也是要把【A.普通通知Notification---->①在“NotificationManager”中设置添加“NotificationChannel”属性】中的代码在执行【startForeground();】语句之前写出来。
Intent intent = new Intent(this, TestActivity.class);
PendingIntent pi = PendingIntent.getActivity(this, 0, intent, 0);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));
...
...
// 【适配Android8.0】设置Notification的Channel_ID,否则不能正常显示
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder.setChannelId("notification_id");
}
...
...
// 额外添加:
// 【适配Android8.0】给NotificationManager对象设置NotificationChannel
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
NotificationChannel channel = new NotificationChannel("notification_id", "notification_name", NotificationManager.IMPORTANCE_LOW);
notificationManager.createNotificationChannel(channel);
}
// 启动前台服务通知
startForeground(1, builder.build());
(本文转至dandelionela主页的https://blog.csdn.net/dandelionela/article/details/86092293)