更新通知通道名称的正确方法是什么?

时间:2017-11-15 14:36:41

标签: android android-8.0-oreo notification-channel

我想根据Locale更新通知频道名称。为此,我正在使用BroadcastReceiver并收听ACTION_LOCALE_CHANGED广播。

我的问题是更新名称的正确方法是什么?

我应该这样做吗?

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

或者我应该重新创建这样的频道吗?

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

通过这样做(第二种方法)我当然会覆盖除了频道名称之外的任何内容吗?

1 个答案:

答案 0 :(得分:5)

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