Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot disable notification vibration in Android 8

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?

like image 290
Adam Varhegyi Avatar asked Jan 15 '18 10:01

Adam Varhegyi


People also ask

How do I turn off vibrate on Android permanently?

Open Settings . Scroll down and select Accessibility. Tap Vibration and haptics. To turn off all vibration effects, tap the toggle switch for Use vibration and haptics to turn it off.

Why does my Samsung phone keep vibrating for no reason?

Go into your phone's Settings. Tap on Notifications. Tap on Advanced Settings, found a lower on the screen. In Advanced Settings, scroll down to Notification Reminders and turn it OFF.

Why does my Android keep vibrating?

If your Android phone keeps vibrating randomly, check your notification settings for each app installed on your device. You can use Nova Launcher to check which of your apps sent the most recent notification. Then install the latest app and Android updates and enable Safe Mode.


1 Answers

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!

like image 158
pcsantana Avatar answered Sep 22 '22 11:09

pcsantana