Disable sound from NotificationChannel

前端 未结 15 1332
时光说笑
时光说笑 2020-11-27 15:50

Today I started targeting API 26 which forced me to use Notification Channels.

My problem is that now on each new notification (including updates to it) an annoying

15条回答
  •  死守一世寂寞
    2020-11-27 16:34

    As far as I have seen, since API 26 (Oreo) it is not possible to change the sound of a notification after it was once created.

        notificationManager.deleteNotificationChannel("channel_id"));
        NotificationChannel notificationChannel = new NotificationChannel(
            "channel_id", "channel_name",
            NotificationManager.IMPORTANCE_HIGH);
        notificationChannel.setSound(null, null);
        notificationManager.createNotificationChannel(notificationChannel);
    

    Even deleting the channel before creation does not help.

    Google documentation says:

    android.app.NotificationManager public void deleteNotificationChannel(String channelId)

    Deletes the given notification channel. If you create a new channel with this same id, the deleted channel will be un-deleted with all of the same settings it had before it was deleted.

    NotificationChannel#setSound() documentation states

    Only modifiable before the channel is submitted to NotificationManager#createNotificationChannel(NotificationChannel)

    Too bad that notificationBuilder.setSound(defaultSoundUri) does not work as well:

    This method was deprecated in API level 26. Use NotificationChannel#setSound(Uri, AudioAttributes) instead.

    Also using support library does not work. So sound is only settable once in the app and changing by the user is only possible in the settings of the notification. For me Ferran Negre's comment did not work. I do not understand why Google made this restriction. Too bad.

提交回复
热议问题