android 通知(关闭声音和震动)

5,270 阅读1分钟

8.0版本以下

//单个设置
mBuilder.setVibrate(null);
mBuilder.setVibrate(new long[]{0l})
mBuilder.setSound(null);
mBuilder.setLights(0, 0, 0);
//默认全部
mBuilder.setDefaults(Notification.DEFAULT_ALL);

按上面代码,实测锤子5.1系统还是会有声音

 mBuilder.setDefaults(NotificationCompat.FLAG_ONLY_ALERT_ONCE);//统一消除声音和震动

8.0版本

//需要在创建CHANNEL时确定
NotificationManager.IMPORTANCE_MIN: 静默;
NotificationManager.IMPORTANCE_HIGH:随系统使用声音或振动 

notificationChannel = new NotificationChannel(CHANNEL_ONE_ID,
                    CHANNEL_ONE_NAME, NotificationManager.IMPORTANCE_MIN);