Android 为通知禁用振动

Android 为通知禁用振动,android,notifications,vibration,android-vibration,Android,Notifications,Vibration,Android Vibration,我正在使用通知编写应用程序。Google开发者指南鼓励开发者提供定制通知的设置(禁用振动,设置通知声音…),所以如果用户这样设置的话,我尝试禁用通知振动 我正在使用NotificationCompat.Builder创建通知,如下所示: NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(Application.getContext()) .setDefaults(

我正在使用通知编写应用程序。Google开发者指南鼓励开发者提供定制通知的设置(禁用振动,设置通知声音…),所以如果用户这样设置的话,我尝试禁用通知振动

我正在使用
NotificationCompat.Builder
创建通知,如下所示:

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);
我尝试了不同的方法来禁用通知:

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);`
我还尝试在结果对象上构建通知和更改值:

Notification notification = notificationBuilder.build();
notification.vibrate = null;
但当通知出现时,手机仍会振动


如何禁用振动通知?

经过长时间的试错,我想我终于明白了问题所在

问题在于此指令
notificationBuilder.setDefaults(Notification.DEFAULT\u ALL)

无论您在设置
DEFAULT\u后传递给
notificationBuilder.setVibrate()
的参数是什么,所有
DEFAULT\u VIBRATE
都将被自动放弃。谷歌一定有人决定给
setDefaults
一个比
setstrimb
更高的优先级

这就是我在应用程序中禁用振动通知的原因:

notificationBuilder.setDefaults(Notification.DEFAULT_LIGHT | Notification.DEFAULT_SOUND)
                   .setVibrate(new long[]{0L}); // Passing null here silently fails
这是可行的,但仅仅为了禁用振动而初始化一个新的
long[]
是不对的。

.setVibrate(null)
对我来说是可行的-这是一个比创建一个不必要的long[]更好的解决方案


结果:设备不振动,LogCat中也没有抱怨声。:)

它们不会停止,因为您使用的是
设置默认值(Notification.DEFAULT\u ALL)
因此,如果您需要停止振动和声音,请删除此行,或者如果您需要使用默认声音和停止振动,我想您必须使用
设置默认值(Notification.DEFAULT\u sound)

notification.vibrate = new long[] { -1 };
这个代码对我有用

private void removeSoundAndVibration(Notification notification) {
        notification.sound = null;
        notification.vibrate = null;
        notification.defaults &= ~DEFAULT_SOUND;
        notification.defaults &= ~DEFAULT_VIBRATE;

此代码来自Notification Compat Api类。这应该行得通,将所有这些添加到您的构建器中。

到2020年:


将通知通道的重要性设置为NotificationManager。重要性\u NONE
对我有效。

您有两种通知通道解决方案

  • 设置“假”模式以禁用振动
  • 设置重要性标志,但灵活性较低(请参见)。小心,它也会影响其他一些事情,比如优先级
  • 因此,您可以使用

    NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
                // no vibration
                channel.setVibrationPattern(new long[]{ 0 });
                channel.enableVibration(true);
    


    我同意,你的解决方案比我的更优雅。不幸的是,当我在三星Galaxy Note上测试时,它对我不起作用。是否引发异常和/或设备是否振动?据我记忆所及,它振动了,但没有抛出异常或任何类型的警告。这似乎有点奇怪,因为我已经查看了通知的源代码——空振动似乎得到了正确处理。输出
    通知.toString()
    时会得到什么?(而且你的deffo没有在任何地方应用默认值?)OP在最初的帖子中明确指出,这个解决方案已经过测试,不起作用。我也在测试它,但它不起作用。我完全理解传递一个变量来告诉设备振动一次0毫秒的严重性,但这仍然不是我见过的最糟糕的修复方法。至少当你思考它在做什么的时候,它是有意义的。我只是想做同样的事情,这里还有几点要说明。无论您的振动设置如何,通知通道的优先级为3或更高也会导致振动发生。此外,android似乎正在缓存通知通道。如果使用NotificationManager.getNotificationChannel(id),您将获得最后一个实例settings@Jesse非常感谢。关于缓存,你是对的。我确实设置了振动(null)
    ,但没有效果,但当我更改频道id时,它起作用了。这在我的设备上不起作用!我仍然感觉到震动,我不会说通知频道正在缓存,但是第一次安装应用时使用的设置会被记住。为什么?假设用户自定义了通知通道设置。然后,具有新频道设置的应用程序更新不应覆盖这些设置。这意味着您只能在更改频道ID(或在测试时卸载应用程序)的情况下在应用程序更新中更改这些设置。结果:用户的自定义设置会记住旧频道,但您已为通知分配了新频道。当然,不要经常这样做,以免惹恼用户!为什么?你能解释一下吗?这根本不起作用!它只需从状态栏中删除所有通知(-1)
     int importance = NotificationManager.IMPORTANCE_LOW;
                NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);