Home » Android » android – NotificationCompat.Builder setLights(); not working

android – NotificationCompat.Builder setLights(); not working

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m trying to use the LED on my notification and it’s not working, i have this code:

NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(getApplicationContext());
nBuilder.setLights(Color.CYAN, 1000, 500);

The entire notification is working, like ContentTitle, ContentText and the notification is shown, but only the LED is not working.

Is there anything wrong with my code ? Should i use the Notification instead NotificationCompact.Builder ?

How to&Answers:

The LED light for notifications is turned on by the OS in the device only if the notification is triggered while the device screen is off.

Answer:

Your code can not work, because you have to pass three variables:

  1. the color
  2. if turning on the led is on
  3. if turning off the led is on

if you enable 2 and 3 your led will be blinking, if you disable 2 and 3 the led will be turned off

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context );
mBuilder.setLights(Color.RED, 1, 1); // will blink

Answer:

That code seems OK, for the APIs before 26.

You might try adding Notification.FLAG_SHOW_LIGHTS

NotificationCompat.Builder nBuilder = new NotificationCompat.Builder(context);
nBuilder.setLights(Color.CYAN, 1000, 500);

Notification notif = nBuilder.build();
notif.flags |= Notification.FLAG_SHOW_LIGHTS;

NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);
notificationManager.notify(0, notif);

Also make sure that you do not swipe down to preview the notification, otherwise the LED will be off when the display goes black.