Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 603
notGeek Avatar asked Nov 15 '17 14:11

notGeek


People also ask

What should be channel ID for a notification in Android?

ChannelId is a unique String to identify each NotificationChannel and is used in Notification. Builder (line 7) when constructing the Notification object. NotificationChannel settings, except channel name and description text, are immutable after it is submitted to NotificationManager at line 5.

How do I set notification channels on Android?

To create a notification channel, follow these steps: Construct a NotificationChannel object with a unique channel ID, a user-visible name, and an importance level. Optionally, specify the description that the user sees in the system settings with setDescription() .

What are channels in notification?

What Are Notification Channels on Android? “Notification Channels” were introduced in 2017 with Android 8.0 Oreo. The idea is that apps can group different types of notifications into “channels.” Each channel can then be turned on or off by the user. This all happens in the Android settings.

What are channel notifications on Android phone?

Starting with Android 8.0, apps are required to assign their notifications to so-called “notification channels”. These channels determine which signals (notification sound, light, vibration, etc.) incoming notifications trigger.


1 Answers

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 they 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
like image 188
steliosf Avatar answered Sep 17 '22 17:09

steliosf