Android 通知通过 NotifiationChannel.enableVibration(false) 振动

标签 android notifications android-8.0-oreo notification-channel

使用 API 26 (Android 8.0) 我们需要定义一个 NotificationChannel对于每个通知。每个 channel 都有自己的中断设置(例如振动、灯光、声音)。

问题: 当我禁用此 channel 的振动并将其部署在 Android 8.0(2017 年 9 月安全更新)手机(Nexus 5X)上时,通知仍会触发振动并自动打开(弹出式)我这样做了未设置并希望禁用。

  1. 我在我的 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);
    }
    
  2. 我为通知设置了 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/

相关文章:

android - 通知和 AlarmManager - 取消我设置的闹钟

android - 类型不匹配。需要 : NotificationCompat. 样式,找到 : Notification. BigPictureStyle

android - Oreo 不支持的字体

android - Gradle:Android支持版本冲突

android - android 的 facebook 连接

java - 如何在特定文本字段中设置主键?

ios - 报亭推送通知未启动应用程序

Android 仅将 View 旋转 90 度

Android 谷歌地图教程

Android Oreo 后台服务在打瞌睡模式下不工作?