Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Android] Allow to change default notification channel name after it's creation #1549

Closed
juancruzgs opened this issue Jul 21, 2020 · 7 comments
Labels

Comments

@juancruzgs
Copy link

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);
        }
    }
@juancruzgs juancruzgs changed the title Allow to change default notification channel name after it's creation [Android] Allow to change default notification channel name after it's creation Jul 21, 2020
@Dallas62
Copy link
Collaborator

Hi @juancruzgs
The version 4.0.0, allow you to delete the channel, also in the next version (currently on dev branch) a new function has been implemented to create custom channels (without triggering a notification).

@Dallas62
Copy link
Collaborator

I was thinking Android SDK doesn't allow this.
As you mentioned, there is some exception such as name/description/lower importance in that case. Since it's not a huge change and this can help to fix basic cases, I made a commit based on your example.
I tested it and everything looks good.

@Dallas62
Copy link
Collaborator

You can test this on dev branch, I will probably release the next version this week-end 😉

@juancruzgs
Copy link
Author

Thank you so much @Dallas62
This will let us change the default channel name by replacing a single line in the AndroidManifest
I just tested in on dev branch and it works like a charm!

Sorry for my mistake comparing strings, I was comparing them like in Kotlin code

@Dallas62
Copy link
Collaborator

No problem, I just remember after the change that in Java this is a reference comparison 😉

Dallas62 added a commit that referenced this issue Aug 3, 2020
* dev: (28 commits)
  Release version 5.0.0.
  Fix behaviour of popInitialNotification and onNotification. Fix foreground value.
  fix popInitialNotification and null pointer
  Apply changes to popInitialNotification.
  make sure data/userInfo is an object before spreading
  keep notification data from fcm while populating data userInfo
  Fix case of small/large icon null.
  Add tests for channel update. #1549
  Update CHANGELOG.md
  [Android] Allow to change default notification channel name after it's creation #1549
  align onNotification on both platforms
  Rename `channelDesc` to `channelDescription`.
  Update code from recent PR for scheduled notifications
  return notification id in onNotification method on iOS
  populate userInfo with id on both platforms
  revert changes
  reflecting userInfo working on both platforms in documentation and example
  "@react-native-community/push-notification-ios": "^1.3.0"
  add data to android notification when scheduling
  Add docs for icon strings
  ...

# Conflicts:
#	README.md
@Dallas62
Copy link
Collaborator

Dallas62 commented Aug 4, 2020

Released 😉

@github-actions
Copy link

github-actions bot commented Aug 5, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 30 days if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the Stale label Aug 5, 2021
@github-actions github-actions bot closed this as completed Sep 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants