使用 API 26 (Android 8.0) 我们需要定义一个 NotificationChannel对于每个通知。每个 channel 都有自己的中断设置(例如振动、灯光、声音)。
问题: 当我禁用此 channel 的振动并将其部署在 Android 8.0(2017 年 9 月安全更新)手机(Nexus 5X)上时,通知仍会触发振动并自动打开(弹出式)我这样做了未设置并希望禁用。
我在我的 MainActivity 中注册了一个 NotificationChannel:
// 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); }
我为通知设置了 NotificationChannel:
notification = new NotificationCompat.Builder(context) .setContentIntent(onClickPendingIntent) .setChannelId(NOTIFICATION_CHANNEL_ID_RUNNING) .setOngoing(true) .setWhen(System.currentTimeMillis()) .setAutoCancel(false) .build();
更新(2017 年 10 月 5 日安全更新)
现在一切都按预期工作,没有变通办法,所以我可以选择 targetSDK 26(之前,我使用 25 来避免这种错误行为)。对于其他版本有类似错误的其他手机尚未收到最新更新的情况,我将下面的解决方法标记为已接受的答案。
最佳答案
这似乎可行:
runningchannel.setVibrationPattern(new long[]{0, 0, 0, 0,0, 0, 0, 0, 0});
是的,这很奇怪
关于Android 通知通过 NotifiationChannel.enableVibration(false) 振动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46465749/