Cannot disable notification vibration in Android 8

前端 未结 5 1479
野趣味
野趣味 2021-02-14 18:35

I try to disable vibration when showing a notification.

Func:

public static Notification buildNotifForUploaderService(Context ctx, String          


        
相关标签:
5条回答
  • 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});
    }
    
    0 讨论(0)
  • 2021-02-14 19:22

    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.

    0 讨论(0)
  • 2021-02-14 19:27

    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!

    0 讨论(0)
  • 2021-02-14 19:30

    This function worked for me. With it I create the notification channel in what fully disable lights, vibration and sound.

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,
                    "Foreground Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
            );
    
            serviceChannel.setVibrationPattern(new long[]{ 0 });
            serviceChannel.enableVibration(true);
            serviceChannel.enableLights(false);
            serviceChannel.setSound(null, null);
    
            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);
        }
    }
    
    0 讨论(0)
  • 2021-02-14 19:32

    Use NotificationManager.IMPORTANCE_LOW for importance value.

    NotificationChannel notificationChannel = new NotificationChannel(
        CHANNEL_ID,
        CHANNEL_NAME,
        NotificationManager.IMPORTANCE_LOW
    );
    
    0 讨论(0)
提交回复
热议问题