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.