Search code examples
javaandroidnotificationschannelled

Notification Channel - Is It Possible to Change LightColor After It's Been Set?


I'm trying to change setLightColor with a colour returned from a JavaScript Interface. Unfortunately, NotificationCompat.Builder(context, CHANNEL_ID).setLights has absolutely no sway on API >= 26, so I can't use Intent.putExtra or anything similar.

Is it even possible to change it after it's already been set? I would like it to be dynamic.

EDIT There seems to be some misconception on what I want. I don't want to touch the Broadcast Reciever. It works just fine. I want to change the notification channel. It's not updating setLightColor(Color.___).

In protected void onCreate

String jobColor = someColor; // Will be filled in by other code - different colour every time
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    CharSequence name = "Channel_Name";
    String description = "Channel_Description";
    int importance = NotificationManager.IMPORTANCE_HIGH;
    NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
    channel.setDescription(description);
    channel.enableLights(true);
    channel.setLightColor(Color.parseColor(jobColor)); // Dynamically set from above
    channel.enableVibration(true);
    NotificationManager notificationManager = getSystemService(NotificationManager.class);
    notificationManager.createNotificationChannel(channel);
}

My BroadcastReciever - setLight is not applicable for API 26 or higher I believe

public class AlarmReceiver extends BroadcastReceiver {
private final String CHANNEL_ID = "some_channel";
@Override
public void onReceive(Context context, Intent intent) {
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0 , new Intent(context, MainPage.class), 0);
    String jobColor = intent.getStringExtra("jobColor");

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context, CHANNEL_ID)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .setSmallIcon(R.drawable.ic_stat_name)
            .setContentTitle("Upcoming Shift!")
            .setContentText("Shift at " + intent.getStringExtra("jobName") + " on " + intent.getStringExtra("jobDate") + " at " + intent.getStringExtra("jobTime"))
            .setStyle(new NotificationCompat.BigTextStyle().bigText("You have a shift at " + intent.getStringExtra("jobName") + " on " + intent.getStringExtra("jobDate") + " at " + intent.getStringExtra("jobTime")))
            .setLights(Color.parseColor(jobColor), 10000, 1000)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true);
    NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
    notificationManager.notify(12345, mBuilder.build());
}
}

Solution

  • You can't change channel color notification, importance etc. programmatically without deleting the channel.

    Because a user may have changed the light color manually.

    To achieve this programmatically to get the channel and create a new channel with a new id. delete the old channel. Your changes will not reflect if create a channel with the previous id

    for reference check WhatsApp application try to change the ringtone from an application and see in a channel at the bottom left x channel is deleted message

    Source Android Doc