From 0da2006f7c85098c195c6f281cd71ab3be1289e5 Mon Sep 17 00:00:00 2001 From: Menardi Date: Tue, 20 Aug 2019 15:40:21 +0000 Subject: [PATCH] Merged in Menardi/cordova-plugin-run-in-background/visibility (pull request #1) Add ability to set Android notification visibility --- README.md | 5 +++-- src/android/ForegroundService.java | 22 ++++++++++++++++++++++ www/background-mode.js | 3 ++- 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 1b04cba..ce81a88 100644 --- a/README.md +++ b/README.md @@ -149,7 +149,8 @@ cordova.plugins.backgroundMode.setDefaults({ allowClose: Boolean, // add a "Close" action to the notification closeIcon: 'power', // An icon shown for the close action closeTitle: 'Close', // The text for the close action - showWhen: Boolean //(Default: true) Show the time since the notification was created + showWhen: Boolean, //(Default: true) Show the time since the notification was created + visibility: String, // Android only: one of 'private' (default), 'public' or 'secret' (see https://developer.android.com/reference/android/app/Notification.Builder.html#setVisibility(int)) }) ``` @@ -174,7 +175,7 @@ Various APIs like playing media or tracking GPS position in background might not ```js cordova.plugins.backgroundMode.on('activate', function() { - cordova.plugins.backgroundMode.disableWebViewOptimizations(); + cordova.plugins.backgroundMode.disableWebViewOptimizations(); }); ``` diff --git a/src/android/ForegroundService.java b/src/android/ForegroundService.java index e4ae337..80223e0 100644 --- a/src/android/ForegroundService.java +++ b/src/android/ForegroundService.java @@ -189,6 +189,7 @@ public class ForegroundService extends Service { String text = settings.optString("text", NOTIFICATION_TEXT); boolean bigText = settings.optBoolean("bigText", false); String subText = settings.optString("subText", ""); + String visibility = settings.optString("visibility", ""); Context context = getApplicationContext(); String pkgName = context.getPackageName(); @@ -224,6 +225,10 @@ public class ForegroundService extends Service { new NotificationCompat.BigTextStyle().bigText(text)); } + if (!visibility.equals("")) { + notification.setVisibility(getVisibility(visibility)); + } + setColor(notification, settings); if (intent != null && settings.optBoolean("resume")) { @@ -309,6 +314,23 @@ public class ForegroundService extends Service { return res.getIdentifier(icon, type, pkgName); } + /** + * Get the visibility constant from a string. + * + * @param visibility one of 'public', 'private', 'secret' + * + * @return The visibility constant if a match is found, 'private' otherwise + */ + private int getVisibility (String visibility) + { + if (visibility.equals("public")) { + return Notification.VISIBILITY_PUBLIC; + } else if (visibility.equals("secret")) { + return Notification.VISIBILITY_SECRET; + } else { + return Notification.VISIBILITY_PRIVATE; + } + } /** * Set notification color if its supported by the SDK. * diff --git a/www/background-mode.js b/www/background-mode.js index 3bfdf86..b1f66a9 100644 --- a/www/background-mode.js +++ b/www/background-mode.js @@ -424,7 +424,8 @@ exports._defaults = { allowClose: false, closeIcon: 'power', closeTitle: 'Close', - showWhen: true + showWhen: true, + visibility: undefined }; /**