From f7e5684a66ae372337e13ed8cbf319066141db75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Wed, 17 Aug 2016 13:55:51 +0200 Subject: [PATCH] Re-add color --- src/android/ForegroundService.java | 38 ++++++++++++++++++++++++++++++ www/background-mode.js | 1 + 2 files changed, 39 insertions(+) diff --git a/src/android/ForegroundService.java b/src/android/ForegroundService.java index 9cc1de5..bbc5554 100644 --- a/src/android/ForegroundService.java +++ b/src/android/ForegroundService.java @@ -29,11 +29,15 @@ import android.content.Context; import android.content.Intent; import android.content.res.Resources; import android.os.Binder; +import android.os.Build; import android.os.IBinder; import android.os.PowerManager; import org.json.JSONObject; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + /** * Puts the service in a foreground state, where the system considers it to be * something the user is actively aware of and thus not a candidate for killing @@ -158,6 +162,8 @@ public class ForegroundService extends Service { .setOngoing(true) .setSmallIcon(getIconResId()); + setColor(notification, settings); + if (intent != null && settings.optBoolean("resume")) { PendingIntent contentIntent = PendingIntent.getActivity( context, NOTIFICATION_ID, intent, @@ -211,4 +217,36 @@ public class ForegroundService extends Service { return resId; } + + /** + * Set notification color if its supported by the SDK. + * + * @param notification + * A Notification.Builder instance + * @param settings + * A JSON dict containing the color definition (red: FF0000) + */ + private void setColor(Notification.Builder notification, + JSONObject settings) { + + String hex = settings.optString("color", null); + + if (Build.VERSION.SDK_INT < 21 || hex == null) + return; + + int aRGB = Integer.parseInt(hex, 16) + 0xFF000000; + + try { + Method setColorMethod = notification.getClass().getMethod( + "setColor", int.class); + + setColorMethod.invoke(notification, aRGB); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } + } } diff --git a/www/background-mode.js b/www/background-mode.js index 0028913..51d1bda 100644 --- a/www/background-mode.js +++ b/www/background-mode.js @@ -58,6 +58,7 @@ exports._defaults = { ticker: 'Running in background', resume: true, silent: false, + color: undefined, icon: 'icon' };