Disable vibration for a notification

2020-02-05 01:34发布

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?

5条回答
来,给爷笑一个
2楼-- · 2020-02-05 02:00

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

查看更多
我想做一个坏孩纸
3楼-- · 2020-02-05 02:06
notification.vibrate = new long[] { -1 };

this code work for me.

查看更多
Luminary・发光体
4楼-- · 2020-02-05 02:09

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.

查看更多
叼着烟拽天下
5楼-- · 2020-02-05 02:09

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

查看更多
手持菜刀,她持情操
6楼-- · 2020-02-05 02:18
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.

查看更多
登录 后发表回答