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
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.
Your code can not work, because you have to pass three variables:
- the color
- if turning on the led is on
- 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
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.