Disable vibration for a notification
Solution 1
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.
Solution 2
In the year 2020:
Setting the importance of the notification channel to NotificationManager.IMPORTANCE_NONE
worked for me.
Solution 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 ...
Solution 4
You have 2 solutions with the notification channel.
- Set a "fake" pattern to disable the vibration.
- 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);
Solution 5
notification.vibrate = new long[] { -1 };
this code work for me.
Related videos on Youtube
nstCactus
Updated on July 09, 2022Comments
-
nstCactus almost 2 years
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?
-
nstCactus over 9 yearsI agree, your solution is way more elegant than mine. Unfortunately it didn't work for me when I tested on a Samsung Galaxy Note.
-
ban-geoengineering over 9 yearsDid an exception get thrown and/or did the device vibrate?
-
nstCactus over 9 yearsAs far as I can remember, it vibrated but didn't throw an exception or any kind of warning.
-
ban-geoengineering over 9 yearsThat 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?) -
nstCactus over 9 yearsQuite 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.
-
Gabriel over 9 yearsI 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.
-
Kyle Sweet almost 8 yearsOP clearly stated in original post that this solution was tested and did not work. I am also testing it and it does not work.
-
pppery about 7 yearsWhy? Could you please explain?
-
Jesse over 5 yearsI 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 settings
-
Undefined over 5 years@Jesse 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 worked -
Steve Moretz about 5 yearsThis doesn't work on my device!I'm getting the vibration still
-
P Kuijpers about 4 yearsI'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!
-
Zhar almost 4 yearsthis does not work at all ! it just remove all the notifications from the status bar (-1)
-
Sandaru over 2 yearsSetting notification channel's importance to NotificationManager.IMPORTANCE_LOW worked for me
-
Admin about 2 yearsAs 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.