8

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?

notGeek
  • 1,152
  • 2
  • 18
  • 33
  • 1
    I second that this notification concept warrants some authoritative best practice documentation. Currently android.com is not it. I see enough documentation that seems copied from other classes without modification. E.g. https://developer.android.com/reference/android/app/NotificationChannel.html#enableVibration(boolean) – Martin Westin Apr 16 '18 at 11:06
  • I really wonder about getNotificationChannel(CHANNEL_ID) too. It need to be wrapped in a try catch. But once you have the object you can call almost all methods causing silent "no-ops". There is very limited utility in having access to the object. The create and delete functions really really seem to imply that they are the only two encouraged actions. "Just create again and I will do magic." and "Delete and I will still retain the (disabled) channel in case you ever want to create it again" Seems like channels could have been specified in the Manifest, right? – Martin Westin Apr 16 '18 at 11:16

1 Answers1

15

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. But even if he hasn't changed that, still the importance won't be updated, and actually that's the purpose of the notification channels. To give freedom to the 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 that:

notificationChannel.setDescription("new description"); //set that before creating the channel
steliosf
  • 2,921
  • 2
  • 29
  • 44
  • 4
    I think it is good to know that lowering importance of a channel will also work, but only if user hasn't changed the importance manually before. – Marat Jan 14 '20 at 14:36