4

I'm working on an app that previously targeted Nougat/API 25 and allowed the user to change the notification light color. This all worked fine, assuming that their device supported the various colours on offer, which my phone - a Samsung Galaxy S8 running Oreo - does (so this isn't a device related issue!).

Since notifications have changed somewhat in Oreo/API 26, I'm struggling to work out the best way to get this functionality to work again. I've added the required code to add a NotificationChannel and create notifications again, and have got it to use the color that the user has set, but only for the first notification they create. After that, even if they change the notification light color setting, and I create the NotificationChannel using the new color, the notification light still uses the old color.

So, for example, if I set the light to be blue after installing the app and get a notification, it'll have a blue light. If I set the light to be green and get a notification, it'll still be blue.

I've found that if I change the value of CHANNEL_ID (i.e. create a completely new channel) then this has the effect of resetting things, so it'll pick up the new user specified color, but again it'll only do it once. I know you can't change settings on a NotificationChannel once it's been created, so I've seen similar answers suggesting that you could delete the NotificationChannel and then recreate it, but this doesn't seem to work for me. I could have a separate channel for each of the different colours available, but that seems a bit overkill and annoying for the user if they want to manage notification settings themselves.

Any ideas/suggestions?

In case it's relevant, the relevant code is below:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

    // Create the NotificationChannel
    CharSequence channelName = context.getString(R.string.channel_name);
    String channelDescription = context.getString(R.string.channel_description);
    int importance = NotificationManager.IMPORTANCE_HIGH;

    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, channelName, importance);
    channel.setDescription(channelDescription);
    channel.enableLights(true);
    channel.setLightColor(Helpers.getNotificationLightColor(context));
    channel.enableVibration(true);

    // register the channel with the system
    mNotificationManager.createNotificationChannel(channel);
}

....

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
    .setSmallIcon(R.drawable.ic_stat_name)
    .setLargeIcon(bitmap)
    .setContentTitle(name)
    .setContentText(messageText)
    .setPriority(NotificationCompat.PRIORITY_HIGH)
    .setAutoCancel(true)
    .setDefaults(NotificationCompat.DEFAULT_VIBRATE | NotificationCompat.DEFAULT_SOUND)
    .setLights(Helpers.getNotificationLightColor(context),500,2000)
    .setColor(context.getResources().getColor(R.color.color_red))
    .extend(wearableExtender);

Helpers.getNotificationLightColor(context) retrieves the relevant color that's saved as a SharedPreference - this is unchanged from the previous version.

  • On this device, where the LED color is controllable, does the user have direct control over the color via the Settings app? (Settings > Apps > All Apps > [your app] > App notifications > [your channel]). On a Nexus 5X, where AFAIK there is no color control, the user can toggle whether the notification blinks the LED but cannot control the color. However, I don't know if that is due to a device limitation in my case. If the answer is "the user can control the color in Settings", my guess is that your best answer is to route the user there, rather than controlling it in-app. – CommonsWare Jun 25 '18 at 22:48
  • BTW, if you don't mind my asking, what device are you testing on? – CommonsWare Jun 25 '18 at 22:49
  • Good question: I'm testing on a Samsung Galaxy S8 running Oreo - have updated the question to specify this. There's nothing on the App Notifications > [My Channel] screen to control the color of the light - have importance, sound and vibrate but not color. – alfredbulbasaur Jun 25 '18 at 22:52
  • OK, I just fired up an S8, and I don't see where they can even toggle the LED blink feature, let alone adjust the color. The delete-and-recreate-the-channel trick is unlikely to work, as Google expects that developers will try to use that to get around user control over notifications. Unfortunately, other than to drop this feature, I don't really have a good solution for you. – CommonsWare Jun 25 '18 at 23:00
  • that was my thinking as well, kind of bypasses one of the points of having a notification channel in the first place. think i might have to drop it - shame but it's not like it's a key feature! – alfredbulbasaur Jun 26 '18 at 06:35
  • You still can do it if you delete the channel and recreate it with a different channel id (no need to create a channel for each color). For example, you can keep an internal counter in preferences and use it to build the channel id ('id_00', 'id_01', etc). Remember to recreate the channel with the same current settings instead of initializing everything. – jmart Jun 26 '18 at 11:15
  • 1
    pretty sure that would work - I was doing it manually for testing and it seemed fine. I guess the downside to that is that if the user has changed the settings for the notification channel (e.g. they don't want it to ever vibrate or it should ignore do not disturb) then these settings would get lost, which would be annoying! – alfredbulbasaur Jun 26 '18 at 20:35

2 Answers2

3

I have found no way to do this, even if you call deleteNotificationChannel.

This is extraordinarily annoying since before you can post any notifications on Oreo you must create the channel. Once you create it you can't change the setLightColor setting, yet the odds of you posting a notification before the user goes into the settings page (and thus declaring their preference) is rather high. Worse, even if you uninstall the app (or clear it's data) and decline to set the color at all during the original channel create it will be retained if you use the same channel name for the same app, and you can't set the color during the individual posting of notification itself (setting it in the builder is ignored.)

This is beyond ridiculous but it's what Google has done; I see the reason to prevent a developer from evading a user's desire to shut off notifications but to refuse to change the light color (especially when you can't do it from the app's notification page in the system settings) is taking things a bit too far.

I have no workaround for it on Oreo that I've been able to figure out....

Tickerguy
  • 91
  • 1
  • 6
0

You can't change channel color notification programmatically without deleting the channel. Full answer is here https://stackoverflow.com/a/53288303/4649644

Fahime Ghasemi
  • 775
  • 9
  • 13