androidandroid-8.0-oreonotification-channel

What is the correct way to update notification channel name?


I want to update notification channel name according to Locale. In order to do that I’m using a BroadcastReceiver and listening for the ACTION_LOCALE_CHANGED broadcast.

My question is what is the right way to update the name?

Should I do something like this?

notificationManager.getNotificationChannel(CHANNEL_ID).setName(“newName”);

Or should I recreate the channel like this?

NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, “newName”, NotificationManager.IMPORTANCE_DEFAULT);
notificationManager.createNotificationChannel(notificationChannel);

By doing this (second approach) am I overriding anything except the channel name of course?


Solution

  • You should recreate the channel just as you created it for the first time. The createNotificationChannel command will create the channel if it hasn't been created yet, and it will update the channel if it has been already created.

    If the channel is already created, then the only thing you can change is the name of the channel and the channel description, nothing else. The importance will be ignored, because the user might have already changed the importance of the channel manually. However, even if they haven't changed that, the importance will still not be updated. That's the purpose of the notification channels e.g. to give freedom to users to manage their channels without the developers messing with them when the app is updated.

    So in summary, by declaring:

    NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, “newName”, NotificationManager.IMPORTANCE_DEFAULT);
    notificationManager.createNotificationChannel(notificationChannel);
    

    in an already created channel, the name of the channel will be updated, but not the importance. If you want to update the channel description as well, you can do that like this:

    notificationChannel.setDescription("new description"); //set that before creating the channel