Cannot disable notification vibration in Android 8

你离开我真会死。 提交于 2019-12-03 16:56:25

Use NotificationManager.IMPORTANCE_LOW for importance value.

NotificationChannel notificationChannel = new NotificationChannel(
    CHANNEL_ID,
    CHANNEL_NAME,
    NotificationManager.IMPORTANCE_LOW
);

Like this answer, do:

mNotificationChannel.setVibrationPattern(new long[]{ 0 }); 
mNotificationChannel.enableVibration(true);

Important 1: even if I set the vibration pattern above, but set enableVibration to false, it vibrates. So, set enableVibration to true!

Important 2: like this another answer, the channel keeps its initial settings, so uninstall and install the app again to apply the changes!

Hope it helps!

Add this line to your code to stop vibration:

notificationChannel.enableVibration(false);
// Above line will disable your vibration for the notification

Also, remove the vibration pattern.

So, your updated code will be:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
    //setting pattern to disable vibrating
    notificationChannel.enableVibration(false);

    notificationBuilder = new NotificationCompat.Builder(ctx, CHANNEL_ID);
} else {
    notificationBuilder = new NotificationCompat.Builder(ctx);
    notificationBuilder.setVibrate(new long[]{0L});
}

The answer of Ahmadul Hoq which can be found here might be helpful.

Basically you have to enable vibration and set the vibration pattern to 0L. There seems to be a bug on Android Oreo which causes this workaround.

EDIT:

If you are using summary notification this might cause the double vibration. I had the same behaviour until I found out that the summary notification which was grouped together with the incoming notification was causing this issue. You can create an extra notification channel for the summary notification and set the importance for this one to "low". This means the channel for the summary notification will be silent and you should only have sound and vibration coming from the normal incoming notifications.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!