alfredbulbasaur
alfredbulbasaur

Reputation: 93

Allowing the user to set Notification light color since Oreo/API 26

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.

Upvotes: 4

Views: 1843

Answers (2)

Fahime Ghasemi
Fahime Ghasemi

Reputation: 1035

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

Upvotes: 0

Tickerguy
Tickerguy

Reputation: 111

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....

Upvotes: 3

Related Questions