Disable vibration for a notification

前端 未结 7 1167
北荒
北荒 2020-12-09 15:11

I\'m writing an app using notification. Google developer guidelines encourages developers to provide settings to customize the notifications (disable vibration, set notifica

7条回答
  •  盖世英雄少女心
    2020-12-09 15:43

    You have 2 solutions with the notification channel.

    1. Set a "fake" pattern to disable the vibration.
    2. Set Importance flag, but less flexible (see https://developer.android.com/training/notify-user/channels#importance). Takes care, it will also impact some other stuff like priority...

    As a result, you can use

    NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
                // no vibration
                channel.setVibrationPattern(new long[]{ 0 });
                channel.enableVibration(true);
    

    Or

     int importance = NotificationManager.IMPORTANCE_LOW;
                NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
            
    

提交回复
热议问题