【发布时间】:2018-03-09 23:55:28
【问题描述】:
对于 API 26 (Android 8.0),我们需要为每个通知定义一个 NotificationChannel。每个通道都有自己的干扰设置(例如振动、灯光、声音)。
问题: 当我禁用此频道的振动并将其部署在 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 来避免这种错误行为)。对于其他版本有类似bug的其他手机尚未收到最新更新的情况,我将下面的解决方法标记为已接受的答案。
【问题讨论】:
-
尝试更改重要性级别?NotificationManager.IMPORTANCE_HIGH
-
感谢您的建议@ArnavM。但是,它不会改变行为(仍然会振动并弹出通知)。你能解释一下为什么你认为这应该有所作为吗?
-
一个相关点要注意不要通过同时使用 enableVibration() 和 setVibrationPattern() 来进行错误的实现,请看这里:proandroiddev.com/…
标签: android notifications android-8.0-oreo notification-channel