安卓通知启用NotificationChannel.enableVibration(false)后无振动

3
使用API 26(Android 8.0)时,我们需要为每个通知定义一个NotificationChannel。每个频道都有自己的中断设置(例如振动、灯光、声音)。
问题: 当我在此频道上禁用振动并在Android 8.0(安全更新2017年9月)手机上部署时(Nexus 5X),通知仍会触发振动并自动打开(弹出),而这不是我设置和想要禁用的。
  1. I register a NotificationChannel in my MainActivity:

    // Register NotificationChannels needed for API 26+ to display notification messages
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel runningChannel = new NotificationChannel(NOTIFICATION_CHANNEL_ID_RUNNING,
                getString(R.string.notification_channel_name_running), NotificationManager.IMPORTANCE_LOW);
        runningChannel.enableLights(false);
        runningChannel.enableVibration(false);
        mNotificationManager.createNotificationChannel(runningChannel);
    }
    
  2. I set the NotificationChannel for the Notification:

    notification = new NotificationCompat.Builder(context)
                .setContentIntent(onClickPendingIntent)
                .setChannelId(NOTIFICATION_CHANNEL_ID_RUNNING)
                .setOngoing(true)
                .setWhen(System.currentTimeMillis())
                .setAutoCancel(false)
                .build();
    

更新(安全更新 2017年10月5日)

现在所有事情都按照预期工作,无需任何解决方法,因此我可以选择目标SDK 26(之前,我使用25来避免这种错误行为)。对于其他版本存在类似错误或其他手机尚未收到最新更新的情况,我将下面的解决方法标记为接受的答案。


尝试更改重要性级别?NotificationManager.IMPORTANCE_HIGH - Arnav M.
感谢您的建议@ArnavM。然而,它并没有改变行为(仍然会震动并弹出通知)。您能解释一下为什么您认为这应该有任何区别吗? - hb0
需要注意的相关问题是,不要同时使用enableVibration()和setVibrationPattern()来进行错误的实现。请参考此处:https://proandroiddev.com/oreo-notification-feature-a-critical-issue-that-could-restart-your-android-phone-ca122fa4d9cb - Ali Asadi
1个回答

3

这似乎可行:

runningchannel.setVibrationPattern(new long[]{0, 0, 0, 0,0, 0, 0, 0, 0});

是的,这很奇怪


谢谢@Piotr,我会在有时间的时候检查它,并在它起作用时将您的答案标记为正确。干杯! - hb0
我的 Nexus 5X 接收了一个系统更新 "2017年10月5日"。在此更新之后,我没有遇到问题中提到的错误行为。因此,我不需要使用你提供的解决方法,但它看起来是合法的,所以我将其标记为正确答案,其他人可能还没有收到最新的 Android 8 更新或在未来版本中遇到这个错误行为。谢谢,伙计! - hb0
2
这对我有用。在修改通知渠道时,始终卸载并安装应用程序。 - krisDrOid
@hb0 我想提醒您,如果您实现不正确,可能会出现问题,请查看此帖子 https://proandroiddev.com/oreo-notification-feature-a-critical-issue-that-could-restart-your-android-phone-ca122fa4d9cb - Ali Asadi
谢谢你的回答,卸载并重新安装应用程序对我有帮助。 - Kevin Ramirez Zavalza
@krisDrOid 谢谢,我一直在想为什么设置震动没有任何变化,直到看到了你的评论并重新安装了应用程序。绝对是救星。 - Kristian

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