Notification setLights() Default Value?

15,386

Solution 1

See the Android source for answer:

<!-- Default color for notification LED. -->
<color name="config_defaultNotificationColor">#ffffffff</color>
<!-- Default LED on time for notification LED in milliseconds. -->
<integer name="config_defaultNotificationLedOn">500</integer>
<!-- Default LED off time for notification LED in milliseconds. -->
<integer name="config_defaultNotificationLedOff">2000</integer>

However different ROMs might have different values for these. For example mine returns 5000 for config_defaultNotificationLedOff. So you might want to fetch them at runtime:

Resources resources = context.getResources(),
          systemResources = Resources.getSystem();
notificationBuilder.setLights(
    ContextCompat.getColor(context, systemResources
        .getIdentifier("config_defaultNotificationColor", "color", "android")),
    resources.getInteger(systemResources
        .getIdentifier("config_defaultNotificationLedOn", "integer", "android")),
    resources.getInteger(systemResources
        .getIdentifier("config_defaultNotificationLedOff", "integer", "android")));

According to diff, these attributes are guaranteed to exist on Android 2.2+ (API level 8+).

Solution 2

@Aleks G that do not help. I have the latest update from the compat libaray. But Eclipse say build() isn avalaible. I dont no why. The docu says yes and you...

This is my current code:

    NotificationCompat.Builder notify = new NotificationCompat.Builder(context);
    notify.setLights(Color.parseColor(led), 5000, 5000);
    notify.setAutoCancel(true);
    notify.setSound(Uri.parse(tone));
    notify.setSmallIcon(R.drawable.ic_stat_kw);
    notify.setContentTitle("Ttiel");
    notify.setContentText("Text");
    Intent showIntent = new Intent(context, Activity_Login.class);
    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, showIntent, 0); 
    notify.setContentIntent(contentIntent);

    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(0, notify.getNotification());

runs perfectly. But not with the default onMS and offMS in setLights() :(

Solution 3

You should be able to do this with:

Notifictaion notf = new Notification.Builder(this).setXXX(...).....build();
notf.ledARGB = <your color>;
notf.ledOnMS = <your value>;  //or skip this line to use default
notf.ledOffMS = <your value>; //or skip this line to use default

Basically, don't use setLights on the notification builder. Instead, build the notification first - then you have access to individual fields for the lights.

Update: this is the actual copy/paste from my sample project, which compiles and works fine on android 2.1 and uses blue colour for LED:

Notification notf = new NotificationCompat.Builder(this)
    .setAutoCancel(true)
    .setTicker("This is the sample notification")
    .setSmallIcon(R.drawable.my_icon)
    .build();
notf.ledARGB = 0xff0000ff;
NotificationManager mNotificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.notify(1, notf);
Share:
15,386
StefMa
Author by

StefMa

Updated on July 25, 2022

Comments

  • StefMa
    StefMa almost 2 years

    I want to create a custom notification. So i want to change the lights and the tone. I use the NotificationCompat.Builder for that.

    Now i want to change the Lights via setLights(); Works fine. But i want set the default value of the onMS and offMS. I haven't find something about that.

    Can anybody help me to find the default values? Here is the documentation for that: http://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setLights(int, int, int)

  • StefMa
    StefMa about 11 years
    Sorry but this is confused. U use Notification notf =.. Builder. That doesn't run. Also i can't call build(). (I use as smallest SDK 8). So i must call builder.getNotification();. But this also do not run...
  • Aleks G
    Aleks G about 11 years
    @StefanM. I updated my answer with the actual code from my sample project. NotificationCompat.Builder.build is available from API level 3.
  • StefMa
    StefMa about 11 years
    Only more confused... build() is'nt available says Eclipse! And the setIcon too...
  • Aleks G
    Aleks G about 11 years
    @StefanM. Sorry, it should be setSmallIcon and setLargeIcon. build is available - and my eclipse happily takes it for API level 7. Maybe you should updated your compatibility library.
  • dsandler
    dsandler about 11 years
    build() was added as a synonym for getNotification() in API 16, but they do the same thing.