LoginSignup
4
1

More than 3 years have passed since last update.

Notification Channelの内容の更新でちょっとハマった話

Posted at

Notification Channelでアプリの前バージョンでsetSoundしていて、
それをしないように更新しても、音が鳴り続けてしまうので、なんでやって調べたところ、
どうやらNotificaion Channelで一度設定した項目はプログラムからは変更できないらしい。

理由は以下、

After you create a notification channel, you cannot change the notification channel's visual and auditory behaviors programmatically—only the user can change the channel behaviors from the system settings.

引用:Create and Manage Notification Channels

要は、ユーザからもチャンネルの設定の変更ができるため、一度作成したチャンネルの動作はプログラムからは変更できないということ。
まあ確かに言う通りで、ユーザの好きな設定にしているのにアプリのアップデートで勝手に変わってはたまったもんじゃない。

それでもどうしても変えたい場合は、一回古いチャンネルを削除して、新しくチャンネルを作り直す必要がある。

Notification.kt
val notificationManager = NotificationManagerCompat.from(context)
notificationManager.deleteNotificationChannel("古いnotificationのchannelId")

注意するのは、deleteするのは古いnotificationのchannelIdだということ。
そして新しく作るNotification Channelはdeleteするのとは違うchannelIdに設定すること。

もちろん実行すると、今までユーザが設定していたチャンネルごと削除されてしまうので、
ユーザが設定していた項目はなかったことになる。
よってオススメはしません。

Notification Channelの設定はよく考えて設定しましょう(戒め)

4
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
1