From cec56db33c3c5e7ee319b8e0804b4725b997dbfc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gear=C3=B3id=20Moroney?= Date: Wed, 3 Jun 2015 16:33:09 +0100 Subject: [PATCH] Added extra settings for Lollipop devices Two settings have been added: - Notification visibility - allows the notification to be made visible on a secure lock screen - Color - used to set the color of the notification circle that contains the icon --- README.md | 17 +++++++---------- src/android/ForegroundService.java | 15 +++++++++++++++ www/background-mode.js | 4 +++- 3 files changed, 25 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index acb5f77..7b4d7f0 100644 --- a/README.md +++ b/README.md @@ -201,21 +201,18 @@ document.addEventListener('deviceready', function () { To indicate that the app is executing tasks in background and being paused would disrupt the user, the plug-in has to create a notification while in background - like a download progress bar. #### Override defaults -The title, ticker and text for that notification can be customized as follows: +The title, ticker, text for that notification can be customized as below. Also, by default the app will come to foreground when tapping on the notification. That can be changed by setting resume to false. On Android 5.0+, the color option will set the background color of the notification circle. Also on Android 5.0+, setting isPublic to true will make the full notification show on a secure lockscreen. + +All of these fields are optional - only override the things you need to. ```javascript cordova.plugins.backgroundMode.setDefaults({ title: String, ticker: String, - text: String -}) -``` - -By default the app will come to foreground when taping on the notification. That can be changed also. - -```javascript -cordova.plugins.backgroundMode.setDefaults({ - resume: false + text: String, + resume: true / false, + color: "#123456", + isPublic: true / false }) ``` diff --git a/src/android/ForegroundService.java b/src/android/ForegroundService.java index a19283c..1490e4b 100644 --- a/src/android/ForegroundService.java +++ b/src/android/ForegroundService.java @@ -33,6 +33,7 @@ import android.app.Service; import android.content.Context; import android.content.Intent; import android.content.res.Resources; +import android.graphics.Color; import android.os.Build; import android.os.Handler; import android.os.IBinder; @@ -141,6 +142,20 @@ public class ForegroundService extends Service { .setOngoing(true) .setSmallIcon(getIconResId()); + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + if(settings.optBoolean("isPublic") == true) { + notification.setVisibility(Notification.VISIBILITY_PUBLIC); + } + + if(!settings.optString("color").equals("")) { + try { + notification.setColor(Color.parseColor(settings.optString("color"))); + } catch (Exception e) { + Log.e("BackgroundMode", settings.optString("color") + " is not a valid color"); + } + } + } + if (intent != null && settings.optBoolean("resume")) { PendingIntent contentIntent = PendingIntent.getActivity( diff --git a/www/background-mode.js b/www/background-mode.js index d4a2836..5ffb3d4 100644 --- a/www/background-mode.js +++ b/www/background-mode.js @@ -65,7 +65,9 @@ exports._defaults = { text: 'Doing heavy tasks.', ticker: 'App is running in background', resume: true, - silent: false + silent: false, + isPublic: false, + color: "" };