13

I try to disable vibration when showing a notification.

Func:

public static Notification buildNotifForUploaderService(Context ctx, String title, String message) {

        Notification notification;
        NotificationCompat.Builder notificationBuilder;

        //If device is Android 8+
        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.setVibrationPattern(new long[]{0L});
            notificationBuilder = new NotificationCompat.Builder(ctx, CHANNEL_ID);
        } else {
            notificationBuilder = new NotificationCompat.Builder(ctx);
            notificationBuilder.setVibrate(new long[]{0L});
        }


        notificationBuilder
                .setContentTitle(title)
                .setContentText(message)
                .setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(), R.mipmap.ic_launcher))
                .setSmallIcon(R.drawable.ic_backup_black_24dp);


        notification = notificationBuilder.build();

        return notification;
    }

I call this on an activity's onCreate() like this:

Notification notification = NotificationHelper.buildNotifForUploaderService(this, "title", "message");
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(1, notification);

It is still vibrating. I test on Android 8 device. I have also tried

 notificationChannel.setVibrationPattern(null);

still not works.

I have

<uses-permission android:name="android.permission.VIBRATE" />

No matter how I define the vibration pattern, like:

new long[]{1000L, 500L, 300L, 1000L};

The vibration does not correspond to my settings. Onyl the default "two short" vibration occurs.

Please help if you can, thanks in advance.

E D I T:

As Avijit Karmakar mentioned, I have added

  notificationChannel.enableVibration(false);

Full code now:

public class MainActivity extends AppCompatActivity {

    final static String CHANNEL_ID = "MY_CHANNEL_ID";
    final static String CHANNEL_NAME = "MY_CHANNEL_NAME";


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Notification notification;
    NotificationCompat.Builder mBuilder;
    NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
        //Disabling vibration!
        notificationChannel.enableVibration(false);
        notificationManager.createNotificationChannel(notificationChannel);
        mBuilder = new NotificationCompat.Builder(this, CHANNEL_ID);

    } else {
        mBuilder = new NotificationCompat.Builder(this);
        mBuilder.setVibrate(new long[]{0L});
    }

    mBuilder.setContentTitle("title")
            .setContentText("message")
            .setSmallIcon(R.drawable.ic_android_black_24dp);

    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
    mBuilder.setLargeIcon(bm);

    notification = mBuilder.build();
    notificationManager.notify(1, notification);
    }
}

It is still vibrating.

I test on Xiaomi Mi A1 (Android 8.0)

Can anybody try this code and help me with the results?

Community
  • 1
  • 1
Adam Varhegyi
  • 11,307
  • 33
  • 124
  • 222
  • simply remove vibrate permission from manifest and try once – Akshay Katariya Jan 15 '18 at 10:34
  • @AkshayKatariya Doesn't work – Adam Varhegyi Jan 15 '18 at 10:35
  • Remember that it's `Only modifiable before the channel is submitted to notify(String, int, Notification)` for the first time. Read here: https://developer.android.com/reference/android/app/NotificationChannel.html#enableVibration(boolean) – Eugen Pechanec Jan 15 '18 at 10:51
  • Possible duplicate of [Notification vibrate issue for android 8.0](https://stackoverflow.com/questions/46402510/notification-vibrate-issue-for-android-8-0) – mike47 Nov 27 '18 at 21:34

5 Answers5

23

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!

pcsantana
  • 599
  • 5
  • 12
  • 2
    remember folks to UNINSTALL the app... I had another application flavor installed and got frustrated until I realised it uses the same channel! – Piotr Z Jan 03 '19 at 13:37
  • 2
    After hours of dealing with this problem **Important 2** worked for me! – Reza Feb 15 '19 at 21:27
  • I would add **Important 3**: call `setSound(null, null)` as per [this answer](https://stackoverflow.com/a/60083773/1346778), since phones on vibrate mode will still vibrate if a sound is present – head in the codes Dec 22 '21 at 05:39
17

Use NotificationManager.IMPORTANCE_LOW for importance value.

NotificationChannel notificationChannel = new NotificationChannel(
    CHANNEL_ID,
    CHANNEL_NAME,
    NotificationManager.IMPORTANCE_LOW
);
Mitsuaki Ishimoto
  • 3,162
  • 2
  • 25
  • 32
  • 1
    Finally! If you think vibration is an intrusive act(is it?, finding the solution was) then this should be accepted answer – Tiko Apr 05 '19 at 12:00
  • Yes, it's works. But after that, the application needs to be reinstalled - see **Important 2** in the [previous](https://stackoverflow.com/a/52207215/8313316) answer. – Gregory Jul 28 '21 at 19:00
1

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});
}
Avijit Karmakar
  • 8,890
  • 6
  • 44
  • 59
1

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.

KraffMann
  • 322
  • 1
  • 4
  • 14
1

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);
    }
}