4

I'm trying to update a notification channel after I create it. Namely, I want to properly set the notification sound, but after I create it. I can't really figure out the proper way to do this.

What I tried was to delete the channel and re-create it but it doesn't seem to be working...

if (notificationManager != null) {
            notificationManager.deleteNotificationChannel(NOTIFICATION_CHANNEL_ID);
            notificationManager.createNotificationChannel(channel);
            System.out.println("Created notification channel" + channel.getSound() + " " + channel.getImportance());
        }
Adrian Olar
  • 2,883
  • 4
  • 35
  • 63
  • Why are trying to update the notification channel if you want to update the sound?. Can you give clarity on this? – Prashanth Verma Sep 17 '18 at 08:56
  • So I create the notification channel but at that time I don't need to set the sound because I need some info that I only get at a later time. When I get the info about what sound I need to set I need to set that on the notification channel. – Adrian Olar Sep 17 '18 at 08:57
  • Why don't you change the sound in NotificationCompat.Builder instead of removing the NotificationChannel. – Kunu Sep 17 '18 at 09:07
  • My goal is to update the channel... The deletion and re-creation was just an idea – Adrian Olar Sep 17 '18 at 09:09
  • Your problem is to set the sound right?. So no need to update the channel, just dont give the property of setSound in the channel, rather give that while you are building the noitification – Prashanth Verma Sep 17 '18 at 09:11
  • I didn't want to add that for each notification I create and I wanted to make use of the notification channel.... – Adrian Olar Sep 17 '18 at 10:56

2 Answers2

9

It is correct like stated in the other answer that you can only alter the name and description of the channel once it has been created. However, as you point out in your code you can delete the channel and then create it again. But if you create the same channel once again but change something, for example the sound, it wont work. I'm assuming Android is preventing that change the same way it does if you try to create it when it already exists. So Android must have a way of keeping track of all the deleted channels (in other words, they are not completely deleted).

If you look at WhatsApp they allow you do change the sounds from within the app and if you investigate a little you'll find that they are indeed deleting and creating channels.

So what can you do? What you can do is that you can alter the ID of the new notification channel. Perhaps adding a big enough random element would prevent you from ever having the same ID twice. Or increment something and store that information within your app (prefs or db or something). If the "recreated" channel has a new ID Android will accept your "changes". Since you're not altering an existing channel, you're creating a completely new one. And if you keep the rest of the user-visible information intact (such as name, description etc) then the user will not notice this but only experience that the sound of that type of notification (channel) was altered from within the app.

Any downsides? Well, a superminor one is that Android will show in the App notification settings how many times a channel has been deleted (to alert the user of "spamming"). I don't think many users care about that. And you're also deviating from the design that Android wants (full user control of the channels) which perhaps might not be desirable.

So from how you describe your usecase I think it's fair to do it this way to accomplish what you want :)

poppe
  • 599
  • 6
  • 13
6

Once a notification channel is created, the user has ultimate control over its settings. As the developer, you can only change the channel's title and description. If you want to recreate the channel, you have to use a different id.

See: https://developer.android.com/training/notify-user/channels

Florian Walther
  • 6,237
  • 5
  • 46
  • 104