lmw
2023-06-06 7a563b559c48b9b339784c25fc5f0adc2ab5154e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package com.sinata.download.utils.notification;
 
import android.annotation.TargetApi;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;
import android.util.Log;
 
import static androidx.core.app.NotificationCompat.VISIBILITY_SECRET;
 
/**
 * author:Created by zhaohaoting on 2018/1/10 11:51
 * email:526309416@qq.com
 * desc:
 */
 
public class AVChatNotificationChannelCompat26 {
    private static String NIM_CHANNEL_ID = "download_tip_channel_001";
    private static final String NIM_CHANNEL_NAME = "download tip channel";
    private static final String NIM_CHANNEL_DESC = "download tip notification";
 
    public static String getNIMChannelId(Context context) {
        /*
         * 适配关键:target 8.0+必须设置一个channel,8.0以下一定要返回null!否则通知栏弹不出
         */
        Log.e("NIM_CHANNEL_ID", NIM_CHANNEL_ID);
        return isBuildAndTargetO(context) ? NIM_CHANNEL_ID : null;
    }
 
    public static NotificationChannel createNIMMessageNotificationChannel(Context context, NotificationManager manager) {
        NotificationChannel channel = null;
        /*
         * 适配关键:只有8.0+的机器才能创建NotificationChannel,否则会找不到类。target 8.0+才需要去创建一个channel,否则就用默认通道即null
         */
        if (!isBuildAndTargetO(context)) {
            return channel;
        }
        if (manager == null) {
            manager = ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE));
        }
        if (manager != null) {
            channel = manager.getNotificationChannel(NIM_CHANNEL_ID); // 已经存在就不要再创建了,无法修改通道配置
            if (channel == null) {
                channel = buildNIMMessageChannel();
                manager.createNotificationChannel(channel);
            }
        }
        return channel;
    }
 
 
    @TargetApi(Build.VERSION_CODES.O)
    private static NotificationChannel buildNIMMessageChannel() {
        NotificationChannel channel = new NotificationChannel(NIM_CHANNEL_ID, NIM_CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
        channel.setDescription(NIM_CHANNEL_DESC);
        channel.enableVibration(false);
        channel.setSound(null, null);
        channel.setShowBadge(false);
        channel.setLockscreenVisibility(VISIBILITY_SECRET);//锁屏显示通知
        channel.setBypassDnd(true);
        return channel;
    }
 
    private static boolean isBuildAndTargetO(Context context) {
        return Build.VERSION.SDK_INT >= Build.VERSION_CODES.O &&
                context.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.O;
    }
}