29

I am basically just experimenting with Android development, and a couple of days ago I came across this app called "Go SMS Pro", which, among other things, can set up notifications in different colors (blue, green, orange, pink and light blue). So, I have tried to do this myself in my own app, however I cannot change neiher the color nor the blinking internal of the LED. I currently use this code:

public class MainActivity extends Activity {
  static final int NOTIFICATION_ID = 1;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(buttonOnClick);
  }

  public OnClickListener buttonOnClick = new OnClickListener() {

    @Override
    public void onClick(View v) {
      String ns = Context.NOTIFICATION_SERVICE;
      NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);

      Notification notification = new Notification(R.drawable.icon, "Hello", System.currentTimeMillis());
      notification.flags = Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_AUTO_CANCEL;
      notification.ledARGB = Color.BLUE;
      notification.ledOnMS = 1000;
      notification.ledOffMS = 300;

      Context context = getApplicationContext();
      CharSequence contentTitle = "My notification";
      CharSequence contentText = "Hello World!";
      Intent notificationIntent = new Intent(MainActivity.this, MainActivity.class);
      PendingIntent contentIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0);

      notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);

      mNotificationManager.notify(NOTIFICATION_ID, notification);
    }
  };
}

But as I said, it doesn't work the way I want it to; instead it just blinks in regular green with the default delay, and not the one I have set in my code.

Can anyone see what is wrong with my code, or know if I have to do something else to achieve this?

Daniel Smith
  • 8,561
  • 3
  • 35
  • 58
Frxstrem
  • 38,761
  • 9
  • 79
  • 119

8 Answers8

12

You can use this code:

private static final int LED_NOTIFICATION_ID= 0; //arbitrary constant

private void RedFlashLight() {
    NotificationManager nm = (NotificationManager) getSystemService( NOTIFICATION_SERVICE);
    Notification notif = new Notification();
    notif.ledARGB = 0xFFff0000;
    notif.flags = Notification.FLAG_SHOW_LIGHTS;
    notif.ledOnMS = 100; 
    notif.ledOffMS = 100; 
    nm.notify(LED_NOTIFICATION_ID, notif);
}

Instead of using ARGB value as the example show, you can use int property inside android.graphics.Color class to get the color as well (e.g. Color.WHITE)

Patrick
  • 33,984
  • 10
  • 106
  • 126
Stuti
  • 1,620
  • 1
  • 16
  • 33
  • I know, I wrote whole of it to you so that you can make that difference in your code. May be it works. As it works on my device – Stuti Jun 09 '11 at 16:33
  • 3
    Well, it doesn't work, I have already tried (before I asked the question)...And also, since LEDs work differently on different devices and you probably haven't tested it on the same device as I have, it's not particularly helpful that it works on your device - this code probably works on many devices, but not mine - that's the problem. – Frxstrem Jun 09 '11 at 16:49
  • Thanks Stuti - works fine for me and I had the exact same problem as the poster (green light was flashing). Frxstrem I suggest you check the ARGB code you're using and compare with what Stuti has written.. – tm_forthefuture Nov 21 '13 at 17:59
  • 2
    What about LED_NOTIFICATION_ID? It should be a builtin constant or what? Thanks, – mircobabini Feb 21 '14 at 10:01
  • No, LED_NOTIFICATION_ID? is a local variable. You can set it as: int LED_NOTIFICATION_ID = 0; – JFL Jun 30 '14 at 09:07
  • Do I need to turn on the camera for this? – Kala J Oct 19 '14 at 15:36
  • @Frxstrem Wich phone did you try? – Becario Senior Jun 30 '17 at 13:50
  • Just for information its been deprecated since API level 26 see my answer below : https://stackoverflow.com/questions/6169291/changing-led-color-for-notifications/45793450#45793450 – Prashant Aug 21 '17 at 09:30
11

Did you try: .setLights(Color.BLUE, 500, 500) ?

Works fine on S3, N5, N4, Nexus one too..

TheDevMan
  • 5,914
  • 12
  • 74
  • 144
  • Just for information its been deprecated since API level 26 see my answer below : https://stackoverflow.com/questions/6169291/changing-led-color-for-notifications/45793450#45793450 – Prashant Aug 21 '17 at 09:31
11

Leds are a quite non-standard feature in android phones. If you depend in them, you will miss a good chunk of the user base (consider, for example, the SGS phones, which do not even have leds).

That said, id the int field ledARGB was not useful, you might need to look into some JNI call from that APK. My guess is that it will have different methods depending on the device in which is running.

Aleadam
  • 40,203
  • 9
  • 86
  • 108
  • I want to implement this https://stackoverflow.com/questions/25479517/change-notification-led-color-if-it-is-previously-set-by-the-creator-application Please guide me how to do that. – Dhruba Bose Aug 25 '14 at 09:30
7

FLAG_SHOW_LIGHTS and Notification.Builder.setLights(int,int,int); are deprecated since Android O ( API level 26 ) If you plan to use this feature in API level 26 or greater have look at NotificationChannel

Example :

NotificationChannel mChannel = new NotificationChannel(id, name, importance);
.....
.....
mChannel.enableLights(true);
// Sets the notification light color for notifications posted to this
// channel, if the device supports this feature.
mChannel.setLightColor(Color.RED);

But in this new implementation You may not have control over LED on milli-seconds & LED off milli-seconds it will be dependent on hardware.

Prashant
  • 4,474
  • 8
  • 34
  • 82
2

Have a look on source below.

NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(ctx)
                        .setPriority(Notification.PRIORITY_HIGH)
                        .setSmallIcon(getNotificationIcon())
                        .setColor(0xff493C7C)
                        .setContentTitle(ctx.getString(R.string.app_name))
                        .setContentText(msg)
                        .setDefaults(Notification.DEFAULT_SOUND)
                        .setLights(0xff493C7C, 1000, 1000)
                        .setStyle(new NotificationCompat.BigTextStyle().bigText(styledMsg));
Ahsanwarsi
  • 1,013
  • 9
  • 18
2

Try using the hex color, include an alpha value and set the defaults to 0:

notification.defaults = 0;
notification.ledARGB = 0xff0000ff;

Also, the notification interface says this:

public int ledARGB
Since: API Level 1

The color of the led. The hardware will do its best approximation.

I'm assuming your hardware has all the colors, but it may not.

Femi
  • 64,273
  • 8
  • 118
  • 148
  • I have tried both setting `notification.defaults` to `0` and `notification.ledARGB` to a hexadecimal value (even though `Color.BLUE` has the constant value of `0xff0000ff` anyway), but neither of those things work. I am also aware that the hardware can do approximation, but I know for sure that it should at least be able to do the colors green, blue, orange and pink. – Frxstrem May 29 '11 at 18:08
  • Quite odd. Does the **Go SMS Pro** app successfully change colors on your handset? I know some handsets have issues with LED notification color changes. – Femi May 29 '11 at 18:12
  • Yes, it does, at least to the colors that is preset within that app (as I mentioned, green, blue, orange and pink). However, I have tried some other apps that are supposed to change the color of the LED, that do not work, and just end up showing a green, blinking light no matter what color they are set to, just as my app does. – Frxstrem May 29 '11 at 18:14
  • I recently noticed that Xiaomi A3 notification led is monochromatic.. i wonder if that will make the app crash or system will try the "best" approximation. lol. – Ezequiel Adrian Jan 07 '21 at 15:28
2

Support for the LED colors is really spotty. Try unplugging your USB cable and making sure that no other app is trying to modify the LED at the same time. Also turn off the screen.

Ed Burnette
  • 1,198
  • 8
  • 13
  • Well, I am pretty confident that neither USB, other apps nor the screen are causing this, because I do get a blinking light, though it is not the blinking rate nor the color that I request for. I think it is rather something in the code itself, since my own code, and many of the apps I find in the market etc., have the problems I describe, while a few other apps do not, and actually work. – Frxstrem Jun 09 '11 at 14:28
  • Is this a Nexus One running 2.3.4 stock, or something else? – Ed Burnette Jun 09 '11 at 18:28
1

I have tried my code below, and the light works fine for me. My mobile is Nexus 6P:

mBuilder.setContentTitle("APP_NAME")
                .setContentText(msg)
                .setContentIntent(PendingIntent.getActivity(mCtxt, UUID.randomUUID().hashCode(), new Intent(mCtxt, ReceivePushActivity.class), Notification.FLAG_AUTO_CANCEL))
                .setWhen(System.currentTimeMillis())
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setAutoCancel(true)
                //.setDefaults(Notification.DEFAULT_ALL)
                .setVibrate(new long[] {0, 1000, 200,1000 })
                .setLights(Color.MAGENTA, 500, 500)
                .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
                .setSmallIcon(R.mipmap.notify_logo);

        Notification ntf = mBuilder.build();
//        ntf.flags = Notification.DEFAULT_ALL;
//        ntf.flags = Notification.FLAG_ONLY_ALERT_ONCE;
//        ntf.flags = Notification.FLAG_AUTO_CANCEL;

        mNotificationManager.notify(notifyId, ntf);

Meaning, remove 'DEFAULT_ALL' settings.

Hal Cheung
  • 141
  • 1
  • 2