Disable vibration for a notification
Asked Answered
C

11

40

I'm writing an app using notification. Google developer guidelines encourages developers to provide settings to customize the notifications (disable vibration, set notification sound...), so I am trying to disable vibration for notifications if the user set it that way.

I am using NotificationCompat.Builder to create the notification, like this:

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(Application.getContext())
            .setDefaults(Notification.DEFAULT_ALL)
            .setPriority(Notification.PRIORITY_MAX)
            .setSmallIcon(R.drawable.ic_launcher)
            .setLargeIcon(largeIconBitmap)
            .setAutoCancel(true)
            .setContentIntent(resultPendingIntent)
            .setContentTitle(title)
            .setContentText(content);

I tried different ways to disable notifications:

notificationBuilder.setVibrate(null);

notificationBuilder.setVibrate(new long[]{0l, 0l});

notificationBuilder.setDefaults(Notification.DEFAULT_ALL | ~Notification.DEFAULT_VIBRATE);

notificationBuilder.setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_SOUND);`

I also tried to build the notification and change values on the resulting object:

Notification notification = notificationBuilder.build();
notification.vibrate = null;

But the phone still vibrates when the notification appears.

How can I disable vibration for notifications?

Cagliostro answered 3/6, 2014 at 6:51 Comment(0)
C
68

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.

Cagliostro answered 3/6, 2014 at 6:51 Comment(5)
I completely understand the grossness of passing in a variable to tell the device to vibrate once for 0 milliseconds, but it still isn't the worst fix I've ever seen. At least it makes sense when you think about what it's doing.Caryloncaryn
I was just trying to do the same thing and there's a couple other points to make here. The priority level of 3 or greater of the notification channel will also cause vibrate to occur, regardless of your vibrate settings. Furthermore, it seems android is caching the notification channels. If you use NotificationManager.getNotificationChannel(id) you will get your last instance settingsRahm
@Rahm thank you! You were right about the caching. I did setVibrate(null) and it had no effect, but when I changed my channel id it workedPh
This doesn't work on my device!I'm getting the vibration stillMordent
I'd not say the notification channel is being cached, but the settings that were used at the first app install are remembered. Why? Imagine a user customised the notification channel settings. Then an app update with new channel settings should not overwrite those settings. This means you can only change these settings in an app update if you change the channel-ID (or just uninstall the app when testing). Result: the user's customised settings are remembered for the old channel, but you have assigned a new channel for the notification. Of course, don't do this often to prevent annoyed users!Depopulate
F
8

In the year 2020:

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

Frig answered 7/4, 2020 at 17:24 Comment(2)
this does not work at all ! it just remove all the notifications from the status bar (-1)Faxen
Setting notification channel's importance to NotificationManager.IMPORTANCE_LOW worked for meAlyose
F
4

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);
        
Faxen answered 27/7, 2020 at 14:6 Comment(0)
I
3

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 ...

Inventor answered 28/1, 2015 at 22:49 Comment(0)
S
1

.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. :)

Sustentacular answered 7/9, 2014 at 14:7 Comment(6)
I agree, your solution is way more elegant than mine. Unfortunately it didn't work for me when I tested on a Samsung Galaxy Note.Cagliostro
Did an exception get thrown and/or did the device vibrate?Sustentacular
As far as I can remember, it vibrated but didn't throw an exception or any kind of warning.Cagliostro
That seems a bit strange as I've had a look through the source code for Notification - grepcode.com/file/repository.grepcode.com/java/ext/… - and null vibrate seems to be handled correctly. What do you get when you output notification.toString() ? (And you're deffo not applying defaults anywhere?)Sustentacular
Quite frankly I don't have time to do some more testing on this issue at the moment. I'll try again your solution when I start working again on that project but it won't be anytime soon, sorry.Cagliostro
OP clearly stated in original post that this solution was tested and did not work. I am also testing it and it does not work.Hoffarth
Y
1
notification.vibrate = new long[] { -1 };

this code work for me.

Yashmak answered 17/2, 2017 at 22:11 Comment(1)
Why? Could you please explain?Over
S
1

Above solutions didnt work but adding mBuilder.setOnlyAlertOnce(true); to my notification builder solved my problem.

 if (mBuilder == null) {
            String channelId = "channel-id";
            String channelName = "Channel Name";
            int importance = NotificationManager.IMPORTANCE_MAX;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
                NotificationChannel mChannel = new NotificationChannel(
                        channelId, channelName, importance);

                mChannel.setSound(null, null);
                mChannel.enableVibration(false);

                notificationManager.createNotificationChannel(mChannel);
            }
            mBuilder = new NotificationCompat.Builder(Application.context, channelId);
            mBuilder.setSmallIcon(R.drawable.ic_ulakbel)
                    .setContentTitle("YOURTITLE")
                    .setAutoCancel(true)
                    .setColor(ContextCompat.getColor(Application.context, R.color.green))
                    .setColorized(true);
            mBuilder.setChannelId(channelId);
            mBuilder.setPriority(1);
            mBuilder.setCustomContentView(notificationLayout);
            mBuilder.setCustomBigContentView(notificationLayout);
            mBuilder.setOnlyAlertOnce(true);
            notificationManager.notify(1452, mBuilder.build());
        }else{
            Notification notification = mBuilder.build();
            notification.flags = Notification.FLAG_ONGOING_EVENT;
            notificationManager.notify(1452,notification);
        }
Smoking answered 27/1, 2022 at 17:7 Comment(0)
M
0
private void removeSoundAndVibration(Notification notification) {
        notification.sound = null;
        notification.vibrate = null;
        notification.defaults &= ~DEFAULT_SOUND;
        notification.defaults &= ~DEFAULT_VIBRATE;

This code is from Notification Compat Api Class. This should work, add all these to your builder.

Munmro answered 18/5, 2018 at 18:59 Comment(0)
P
0
   private void createNotificationChannel() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel serviceChannel = new NotificationChannel(
                CHANNEL_ID,
                "Example Service Channel",
                NotificationManager.IMPORTANCE_MIN
        );
        serviceChannel.setVibrationPattern(new long[]{ 0 });
        serviceChannel.enableVibration(true);
        serviceChannel.enableLights(false);
        serviceChannel.setSound(null, null);
        serviceChannel.setShowBadge(false); // 
        NotificationManager manager = getSystemService(NotificationManager.class);
        manager.createNotificationChannel(serviceChannel);
    }

If you make IMPORTANCE_MIN that you can disabled vibration, if you make IMPORTANCE_DEFAULT it happens vibration so you can try IMPORTANCE_MIN

Patman answered 3/4, 2022 at 15:32 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Leech
S
0

July 2022: I have tried everything in this thread, and the only thing that worked was Zhar's suggestion to set importance to low:

int importance = NotificationManager.IMPORTANCE_LOW;
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);

I am targeting Android API level 24.

Salmi answered 13/7, 2022 at 19:22 Comment(0)
D
0

setVibrate(new long[0]) works if you're targetting API 28. I'm working with chinese smartwatches running smartphone android roms so in the rest of the devices should work fine. Hope it helps!

Delbert answered 30/1, 2023 at 18:34 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.