-
-
Notifications
You must be signed in to change notification settings - Fork 2k
[Android] Allow to change default notification channel name after it's creation #1549
Description
Feature Request
The library doesn't allow to change the default notification channel name after it is created.
We are changing this value in the AndroidManifest.xml but it won't do anything if the channel is already created in the device.
<meta-data android:name="com.dieam.reactnativepushnotification.notification_channel_name"
android:value="New channel name />
The user would have to re-install the app to see the change.
A workaround would be to either delete the channel and wait until the library re-creates it or get all the channels from the NotificationManager and change it in our own.
Why it is needed
In our case we need to change the channel name because it was initially created with the wrong name.
But it's also useful to change the channel name if the device locale changes.
Possible implementation
The NotificationManager will update the channel name if it's already created when calling NotificationManager#createNotificationChannel(channel).
Documentation:
* Creates a notification channel that notifications can be posted to.
*
* This can also be used to restore a deleted channel and to update an existing channel's
* name, description, group, and/or importance.
*
* <p>The name and description should only be changed if the locale changes
* or in response to the user renaming this channel. For example, if a user has a channel
* named 'John Doe' that represents messages from a 'John Doe', and 'John Doe' changes his name
* to 'John Smith,' the channel can be renamed to match.
*
* <p>The importance of an existing channel will only be changed if the new importance is lower
* than the current value and the user has not altered any settings on this channel.
*
* <p>The group an existing channel will only be changed if the channel does not already
* belong to a group.
*
* All other fields are ignored for channels that already exist.
*
* @param channel the channel to create. Note that the created channel may differ from this
* value. If the provided channel is malformed, a RemoteException will be
* thrown.
*/
public void createNotificationChannel(@NonNull NotificationChannel channel) {
createNotificationChannels(Arrays.asList(channel));
}
You could change the channel == null check from this code and call manager#createNotificationChannel(channel) if the name/description changes.
private void checkOrCreateChannel(NotificationManager manager, String channel_id, String channel_name, String channel_description, Uri soundUri, int importance, long[] vibratePattern) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
return;
if (manager == null)
return;
NotificationChannel channel = manager.getNotificationChannel(channel_id);
if (channel == null) {
channel = new NotificationChannel(channel_id, channel_name, importance);
// Sets channel properties...
manager.createNotificationChannel(channel);
}
}
Code sample
RNPushNotificationHelper line 902
private void checkOrCreateChannel(NotificationManager manager, String channel_id, String channel_name, String channel_description, Uri soundUri, int importance, long[] vibratePattern) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
return;
if (manager == null)
return;
NotificationChannel channel = manager.getNotificationChannel(channel_id);
if(channel_name == null) {
channel_name = this.config.getChannelName(channel_id);
}
if(channel_description == null) {
channel_description = this.config.getChannelDescription(channel_id);
}
if (channel == null || channel.getName() != channel_name || channel.getDescription() != channel_description) {
// If channel or doesn't exist create a new one.
// If channel name or description is updated then update the existing channel.
channel = new NotificationChannel(channel_id, channel_name, importance);
channel.setDescription(channel_description);
channel.enableLights(true);
channel.enableVibration(true);
channel.setVibrationPattern(vibratePattern);
if (soundUri != null) {
AudioAttributes audioAttributes = new AudioAttributes.Builder()
.setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
.setUsage(AudioAttributes.USAGE_NOTIFICATION)
.build();
channel.setSound(soundUri, audioAttributes);
} else {
channel.setSound(null, null);
}
manager.createNotificationChannel(channel);
}
}