NotificationChannel

 
通知渠道的引入可以很方便的管理,和归纳同一种类型的Notification。以前多个通知 ,无论是不是同一个应用的通知,逐个排列下来,占满了屏幕,不太友好。
1. 相同通知渠道的通知已经被合并,而不是一一全部展开。
2. 如果你的项目TargetSDK26以上,那么你在使用Notification的时候必须指定一个ChannelId,否则当然会报错
3. 下面是Android 8.0上通知渠道NotificationChannel 的使用代码段,注意点需要传入CHANNEL_ID(随意指定),CHANNEL_NAME(随意指定)
    public static NotificationManager notificationManager;
    public static String CHANNEL_1 = "channel1";

    public static void init(Context context){
        notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
       ////配置通知渠道id,渠道名称(用户可以看到),渠道优先级 NotificationChannel channel1
= new NotificationChannel(CHANNEL_1,"通知渠道1", NotificationManager.IMPORTANCE_HIGH); channel1.setDescription("通知渠道的描述1"); ///配置通知出现时的闪灯(如果 android 设备支持的话)
       channel1.enableLights(
true); channel1.setLightColor(Color.WHITE);
       
//配置通知出现时的震动(如果 android 设备支持的话)
channel1.enableVibration(true);
channel1.setVibrationPattern(new long[]{100, 200, 100, 200});
//channel.setBypassDnd(true); //设置绕过免打扰模式
//channel.canBypassDnd(); //检测是否绕过免打扰模式
//channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);//设置在锁屏界面上显示这条通知

          notificationManager.createNotificationChannel(channel1);

        }

    }

可以通过notificationManager.deleteNotificationChannel(String channelId)去删除指定channelId的渠道。

更多设置可百度相关需求

原文地址:https://www.cnblogs.com/yangjj08/p/10279169.html