Skip to content

Commit

Permalink
Android lights/LED support
Browse files Browse the repository at this point in the history
  • Loading branch information
Benjamin-Dobell committed Jul 20, 2017
1 parent f084ebd commit 59c6237
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ protected PendingIntent getCTAPendingIntent() {

protected Notification.Builder getNotificationBuilder(PendingIntent intent) {
final Integer icon = mNotificationProps.getIcon();
final Integer color = mNotificationProps.getColor();

final Notification.Builder builder = new Notification.Builder(mContext)
.setContentTitle(mNotificationProps.getTitle())
Expand All @@ -141,10 +140,20 @@ protected Notification.Builder getNotificationBuilder(PendingIntent intent) {
.setDefaults(Notification.DEFAULT_ALL)
.setAutoCancel(true);

final Integer color = mNotificationProps.getColor();

if (color != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder.setColor(color);
}

final Integer lightsColor = mNotificationProps.getLightsColor();
final Integer lightsOnMs = mNotificationProps.getLightsOnMs();
final Integer lightsOffMs = mNotificationProps.getLightsOffMs();

if (lightsColor != null && lightsOnMs != null && lightsOffMs != null) {
builder.setLights(lightsColor, lightsOnMs, lightsOffMs);
}

return builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.google.firebase.messaging.RemoteMessage;

import java.util.Map;
import java.util.Objects;

public class NotificationProps {

Expand All @@ -27,6 +28,9 @@ public class NotificationProps {
// Local-only support

private static final String LARGE_ICON = "largeIcon"; // Drawable name or URL
private static final String LIGHTS_COLOR = "lightsColor";
private static final String LIGHTS_ON_MS = "lightsOnMs";
private static final String LIGHTS_OFF_MS = "lightsOffMs";

public static NotificationProps fromRemoteMessage(Context context, RemoteMessage remoteMessage) {
final Bundle properties = new Bundle();
Expand Down Expand Up @@ -123,6 +127,21 @@ public String getLargeIcon() {
return mProperties.getString(LARGE_ICON);
}

@Nullable
public Integer getLightsColor() {
return colorFromString(mProperties.getString(LIGHTS_COLOR));
}

@Nullable
public Integer getLightsOnMs() {
return getInteger(LIGHTS_ON_MS);
}

@Nullable
public Integer getLightsOffMs() {
return getInteger(LIGHTS_OFF_MS);
}

@Nullable
public Bundle getData() {
return mProperties.getBundle(DATA);
Expand All @@ -141,6 +160,30 @@ public String toString() {
return sb.toString();
}

@Nullable
private Integer getInteger(String key) {
final Object object = mProperties.get(key);

if (object instanceof Number) {
return ((Number) object).intValue();
}

return integerFromString(mProperties.getString(key));
}

@Nullable
private Integer integerFromString(String string) {
if (string != null) {
try {
return Integer.parseInt(string);
} catch (NumberFormatException e) {
// Move on
}
}

return null;
}

@Nullable
private Integer colorFromString(String string) {
if (string != null) {
Expand Down
12 changes: 12 additions & 0 deletions notification.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,16 @@ export default class NotificationAndroid {
getLargeIcon() {
return this.properties.largeIcon;
}

getLightsColor() {
return this.properties.lightsColor;
}

getLightsOnMs() {
return this.properties.lightsOnMs;
}

getLightsOffMs() {
return this.properties.lightsOffMs;
}
}

0 comments on commit 59c6237

Please sign in to comment.