如何禁用通知中的声音(Android O或更高版本中的通知)

And*_*Lam 4 android

我的应用程序要求是在玩家状态更改时更新媒体样式通知.之前完美地工作,触发并显示mediaSession的新媒体类型通知,没有声音或振动.

现在问题:在根据Android O要求构建通知渠道时,我使用以下代码创建通知渠道.然后令人讨厌的问题是,每次媒体会话改变时,每个通知都会更新,在Android O中现在播放通知声音.

我想为每个新通知禁用声音,如果我没有设置声音,则默认声音触发,在两个字段中传入null都不起作用.

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        NotificationChannel notificationChannel = new NotificationChannel(
                NOTIFICATION_CHANNEL_ID,
                "SimpleBakingApp Media Notification",
                NotificationManager.IMPORTANCE_LOW
        );

        // Configure the notification channel.
        notificationChannel.setDescription("Channel description");
        notificationChannel.setSound(null,null); // <-- Is there a way to disable sound? null doesn't work
        notificationChannel.enableLights(true);
        notificationChannel.setLightColor(Color.RED);
        notificationChannel.enableVibration(false);
        mNotificationManager.createNotificationChannel(notificationChannel);
    }
Run Code Online (Sandbox Code Playgroud)

额外的信息,可能是相关的

我的showNotification()(构建通知的方法)触发Player.EventListener回调中的玩家状态更改,我正在使用ExoPlayer v2.

kco*_*ock 9

你在寻找什么setOnlyAlertOnce(boolean).

当您创建通知时,true如果您只希望第一次显示该通知(而不是对同一通知的后续更新)来触发声音和/或振动,则可以将此设置为.

  • 谢谢,我已经设置了IMPORTANCE_LOW,但没有在测试设备上重新安装应用程序.现在我重新安装了应用程序,声音消失了,谢谢. (4认同)
  • 不幸的是,在您创建频道后,通知频道就无法使用了。创建后,您无法在事后修改这些属性。您可以为这些无声通知创建一个额外的频道,并且不要在该频道上调用 setSound()。您可能还必须将其设置为 IMPORTANCE_LOW。 (2认同)
  • 重新安装该应用程序对我来说都可行 (2认同)