Android 8.0 的通知震动问题

9
我创建了一个通知渠道,以在Android 8.0上显示通知,如下所示。
 NotificationChannel uploadChannel = new NotificationChannel(Constants.NOTIFICATION_CHANNEL_UPLOAD_ID,"Uploads", NotificationManager.IMPORTANCE_LOW);
 uploadChannel.enableLights(false);
 uploadChannel.setDescription("Uploads Channel");
 notificationManager.createNotificationChannel(uploadChannel);

每次我展示通知时,手机都会震动多次。 我通过以下方式禁用了通知渠道的震动功能。
uploadChannel.enableVibration(false);

我创建了以下通知:
 NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, Constants.NOTIFICATION_CHANNEL_UPLOAD_ID);

但它并没有起作用,手机在每个通知时都会震动。

5个回答

25

除了Ahmadul Hoq的回答,对我也有用,我想补充一点,可能需要在震动改变生效之前卸载并重新安装应用程序。如果应用程序被终止,则通道保留其初始设置并保持活动状态 - 因此,如果您仅构建和运行,则可能不会应用更改。


这是否意味着我们不能再让用户更改通知震动设置了?我想知道 Whatsapp 是如何做到的? - behelit
@behelit 开发人员很可能会应用Admadul的更改,运行应用程序并测试结果。在这种特定情况下,更改不直接应用,这在开发过程中很令人困惑。我相当确定您也可以重新启动设备并产生类似的效果。我认为这并不意味着用户不能再更改通知震动设置了。 - Roland van der Linden
4
您可以选择仅 清除应用程序数据 - Kathir
2
如果您想将通知渠道所做的更改应用于生产环境和当前安装了您的应用程序的用户,请将您的渠道ID更改为新的ID。 - Mostafa Arian Nejad

24

这是Android 8通知系统上的一个错误。我已经尝试过不同组合的enableVibrationsetVibrationPattern(null),但都没有效果。

唯一让震动停止的解决方案是:

mNotificationChannel.setVibrationPattern(new long[]{ 0 });
mNotificationChannel.enableVibration(true);

需要注意的是,即使我将振动模式设置为0,但如果将enableVibration设置为false,它仍然会发生振动。


4
如果无法正常工作,请参考Roland van der Linden的回答 - Poorya
1
他们修复了吗?这个黑客能一直工作到下一个“P”版本吗? - Kirill Reznikov
1
@KirillReznikov 刚在 Android 9 上测试了一下,这个黑客攻击仍然有效,而且他们还没有修复它... - Ahmadul Hoq

2

2

这个函数对我有效。使用它可以创建一个通知渠道,其中完全禁用了灯光、震动和声音。

    private void createNotificationChannel() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel serviceChannel = new NotificationChannel(
                    CHANNEL_ID,
                    "Foreground Service Channel",
                    NotificationManager.IMPORTANCE_DEFAULT
            );

            serviceChannel.setVibrationPattern(new long[]{ 0 });
            serviceChannel.enableVibration(true);
            serviceChannel.enableLights(false);
            serviceChannel.setSound(null, null);

            NotificationManager manager = getSystemService(NotificationManager.class);
            manager.createNotificationChannel(serviceChannel);
        }
    }

1

添加 notificationChannel.setVibrationPattern(new long[]{ 0L });。 如果出现“channel_id already registered for application”错误,可以尝试卸载应用程序或清除应用数据,然后再次运行它。这样就可以解决问题了!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接