Disable vibration for a notification

前端 未结 7 1148
北荒
北荒 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);
            
    
    0 讨论(0)
  • 2020-12-09 15:55

    .setVibrate(null) works for me - and a better solution than creating a needless long[].

    Result: device doesn't vibrate and no grumbling in LogCat either. :)

    0 讨论(0)
  • 2020-12-09 15:58

    They are not stop because you are use "setDefaults(Notification.DEFAULT_ALL)" so if you need to stop vibration and sound remove this line , or if you need to use the default sound and stop vibration I think you must use setDefaults(Notification.DEFAULT_SOUND) etc ...

    0 讨论(0)
  • 2020-12-09 16:02

    After a long trial & error session, I think I finally understood what's wrong.

    The problem lies in this instruction notificationBuilder.setDefaults(Notification.DEFAULT_ALL).

    No matter what parameter you pass to notificationBuilder.setVibrate() after setting DEFAULT_ALL or DEFAULT_VIBRATE will be silently discarded. Someone at Google must have decided to give a higher precedence to setDefaults than to setVibrate.

    This is how I ended up disabling vibration for notifications in my app:

    notificationBuilder.setDefaults(Notification.DEFAULT_LIGHT | Notification.DEFAULT_SOUND)
                       .setVibrate(new long[]{0L}); // Passing null here silently fails
    

    This works but doesn't feel right to initialize a new long[] just to disable the vibration.

    0 讨论(0)
  • 2020-12-09 16:02

    In the year 2020:

    Setting the importance of the notification channel to NotificationManager.IMPORTANCE_NONE worked for me.

    0 讨论(0)
  • 2020-12-09 16:03
    notification.vibrate = new long[] { -1 };
    

    this code work for me.

    0 讨论(0)
提交回复
热议问题