diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..485dee64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/README.md b/README.md index bde896a8..140c1149 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ +## Deprecated by the original author + +This fork lives on here: https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background + +This readme is left for historical purposes. +

SAMPLE APP :point_right: @@ -145,11 +151,18 @@ The title, text and icon for that notification can be customized as below. Also, cordova.plugins.backgroundMode.setDefaults({ title: String, text: String, - icon: 'icon' // this will look for icon.png in platforms/android/res/drawable|mipmap - color: String // hex format like 'F14F4D' + subText: String, // see https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setSubText(java.lang.CharSequence) + icon: 'icon', // this will look for icon.png in platforms/android/res/drawable|mipmap + color: String, // hex format like 'F14F4D' resume: Boolean, hidden: Boolean, - bigText: Boolean + bigText: Boolean, + channelName: String, // Shown when the user views the app's notification settings + channelDescription: String, // Shown when the user views the channel's settings + 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 }) ``` @@ -205,4 +218,4 @@ Made with :yum: from Leipzig [changelog]: CHANGELOG.md [apache2_license]: http://opensource.org/licenses/Apache-2.0 [appplant]: http://appplant.de -[meshfields]: http://meshfields.de \ No newline at end of file +[meshfields]: http://meshfields.de diff --git a/plugin.xml b/plugin.xml index 2b774405..7d0a54ce 100644 --- a/plugin.xml +++ b/plugin.xml @@ -24,7 +24,6 @@ - @@ -77,6 +76,7 @@ + - - - + + + + + + + diff --git a/src/android/BackgroundMode.java b/src/android/BackgroundMode.java index cac418af..ae8ada5a 100644 --- a/src/android/BackgroundMode.java +++ b/src/android/BackgroundMode.java @@ -22,13 +22,14 @@ Licensed to the Apache Software Foundation (ASF) under one package de.appplant.cordova.plugin.background; import android.app.Activity; -import android.content.ComponentName; -import android.content.Intent; -import android.content.ServiceConnection; +import android.content.*; +import android.os.Bundle; import android.os.IBinder; import org.apache.cordova.CallbackContext; +import org.apache.cordova.CordovaInterface; import org.apache.cordova.CordovaPlugin; +import org.apache.cordova.CordovaWebView; import org.json.JSONArray; import org.json.JSONObject; @@ -77,6 +78,24 @@ public void onServiceDisconnected (ComponentName name) } }; + @Override + public void initialize(CordovaInterface cordova, CordovaWebView webView) { + super.initialize(cordova, webView); + IntentFilter filter = new IntentFilter(); + filter.addAction("com.backgroundmode.close" + cordova.getContext().getPackageName()); + cordova.getActivity().registerReceiver(receiver, filter); + + } + + private BroadcastReceiver receiver = new BroadcastReceiver() { + + @Override + public void onReceive(Context context, Intent intent) { + cordova.getActivity().finish(); + + } + }; + /** * Executes the request. * @@ -284,7 +303,7 @@ private void fireEvent (Event event, String params) String str = String.format("%s._setActive(%b)", JS_NAMESPACE, active); - str = String.format("%s;%s.on%s(%s)", + str = String.format("%s;%s.on('%s', %s)", str, JS_NAMESPACE, eventName, params); str = String.format("%s;%s.fireEvent('%s',%s);", diff --git a/src/android/ForegroundService.java b/src/android/ForegroundService.java index 806c9568..e4ae3371 100644 --- a/src/android/ForegroundService.java +++ b/src/android/ForegroundService.java @@ -23,20 +23,17 @@ Licensed to the Apache Software Foundation (ASF) under one import android.annotation.SuppressLint; import android.annotation.TargetApi; -import android.app.Notification; -import android.app.NotificationManager; -import android.app.PendingIntent; -import android.app.Service; +import android.app.*; import android.content.Context; import android.content.Intent; import android.content.res.Resources; +import android.graphics.drawable.Icon; import android.os.Binder; import android.os.Build; import android.os.IBinder; import android.os.PowerManager; -import android.app.NotificationChannel; - import org.json.JSONObject; +import android.support.v4.app.NotificationCompat; import static android.os.PowerManager.PARTIAL_WAKE_LOCK; @@ -173,38 +170,49 @@ private Notification makeNotification (JSONObject settings) { // use channelid for Oreo and higher String CHANNEL_ID = "cordova-plugin-background-mode-id"; - if(Build.VERSION.SDK_INT >= 26){ - // The user-visible name of the channel. - CharSequence name = "cordova-plugin-background-mode"; - // The user-visible description of the channel. - String description = "cordova-plugin-background-moden notification"; + if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + // The user-visible name of the channel. + CharSequence name = settings.optString("channelName", "cordova-plugin-background-mode"); + // The user-visible description of the channel. + String description = settings.optString("channelDescription", "cordova-plugin-background-moden notification"); - int importance = NotificationManager.IMPORTANCE_LOW; + int importance = NotificationManager.IMPORTANCE_LOW; - NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name,importance); + NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance); - // Configure the notification channel. - mChannel.setDescription(description); + // Configure the notification channel. + mChannel.setDescription(description); - getNotificationManager().createNotificationChannel(mChannel); + getNotificationManager().createNotificationChannel(mChannel); } String title = settings.optString("title", NOTIFICATION_TITLE); String text = settings.optString("text", NOTIFICATION_TEXT); boolean bigText = settings.optBoolean("bigText", false); + String subText = settings.optString("subText", ""); Context context = getApplicationContext(); String pkgName = context.getPackageName(); Intent intent = context.getPackageManager() .getLaunchIntentForPackage(pkgName); - Notification.Builder notification = new Notification.Builder(context) + NotificationCompat.Builder notification = new NotificationCompat.Builder(context, CHANNEL_ID) .setContentTitle(title) .setContentText(text) .setOngoing(true) - .setSmallIcon(getIconResId(settings)); + .setSmallIcon(getIconResId(settings)) + .setShowWhen(settings.optBoolean("showWhen", true)); + + if (!subText.equals("")) { + notification.setSubText(subText); + } + + if (settings.optBoolean("allowClose", false)) { - if(Build.VERSION.SDK_INT >= 26){ - notification.setChannelId(CHANNEL_ID); + final Intent clostAppIntent = new Intent("com.backgroundmode.close" + pkgName); + final PendingIntent closeIntent = PendingIntent.getBroadcast(context, 1337, clostAppIntent, 0); + final String closeIconName = settings.optString("closeIcon", "power"); + NotificationCompat.Action.Builder closeAction = new NotificationCompat.Action.Builder(getIconResId(closeIconName), settings.optString("closeTitle", "Close"), closeIntent); + notification.addAction(closeAction.build()); } if (settings.optBoolean("hidden", true)) { @@ -213,7 +221,7 @@ private Notification makeNotification (JSONObject settings) if (bigText || text.contains("\n")) { notification.setStyle( - new Notification.BigTextStyle().bigText(text)); + new NotificationCompat.BigTextStyle().bigText(text)); } setColor(notification, settings); @@ -251,23 +259,40 @@ protected void updateNotification (JSONObject settings) } /** - * Retrieves the resource ID of the app icon. + * Retrieves the resource ID of the sent icon name * - * @param settings A JSON dict containing the icon name. + * @param name Name of the resource to return */ - private int getIconResId (JSONObject settings) - { - String icon = settings.optString("icon", NOTIFICATION_ICON); + private int getIconResId(String name) { + int resId = getIconResId(name, "mipmap"); - int resId = getIconResId(icon, "mipmap"); + if (resId == 0) { + resId = getIconResId(name, "drawable"); + } if (resId == 0) { - resId = getIconResId(icon, "drawable"); + resId = getIconResId("icon", "mipmap"); } + if (resId == 0) { + resId = getIconResId("icon", "drawable"); + } + + return resId; } + /** + * Retrieves the resource ID of the app icon. + * + * @param settings A JSON dict containing the icon name. + */ + private int getIconResId(JSONObject settings) { + String icon = settings.optString("icon", NOTIFICATION_ICON); + + return getIconResId(icon); + } + /** * Retrieve resource id of the specified icon. * @@ -281,13 +306,7 @@ private int getIconResId (String icon, String type) Resources res = getResources(); String pkgName = getPackageName(); - int resId = res.getIdentifier(icon, type, pkgName); - - if (resId == 0) { - resId = res.getIdentifier("icon", type, pkgName); - } - - return resId; + return res.getIdentifier(icon, type, pkgName); } /** @@ -297,8 +316,8 @@ private int getIconResId (String icon, String type) * @param settings A JSON dict containing the color definition (red: FF0000) */ @TargetApi(Build.VERSION_CODES.LOLLIPOP) - private void setColor (Notification.Builder notification, JSONObject settings) - { + private void setColor(NotificationCompat.Builder notification, + JSONObject settings) { String hex = settings.optString("color", null); diff --git a/src/android/res/drawable-hdpi/power.png b/src/android/res/drawable-hdpi/power.png new file mode 100644 index 00000000..31b32c87 Binary files /dev/null and b/src/android/res/drawable-hdpi/power.png differ diff --git a/src/android/res/drawable-mdpi/power.png b/src/android/res/drawable-mdpi/power.png new file mode 100644 index 00000000..78a474b8 Binary files /dev/null and b/src/android/res/drawable-mdpi/power.png differ diff --git a/src/android/res/drawable-xhdpi/power.png b/src/android/res/drawable-xhdpi/power.png new file mode 100644 index 00000000..9026845a Binary files /dev/null and b/src/android/res/drawable-xhdpi/power.png differ diff --git a/src/android/res/drawable-xxhdpi/power.png b/src/android/res/drawable-xxhdpi/power.png new file mode 100644 index 00000000..812b1a4d Binary files /dev/null and b/src/android/res/drawable-xxhdpi/power.png differ diff --git a/src/android/res/drawable/power.xml b/src/android/res/drawable/power.xml new file mode 100644 index 00000000..1f717634 --- /dev/null +++ b/src/android/res/drawable/power.xml @@ -0,0 +1,9 @@ + + + diff --git a/src/ios/APPBackgroundMode.m b/src/ios/APPBackgroundMode.m index 20f0695b..174b6667 100644 --- a/src/ios/APPBackgroundMode.m +++ b/src/ios/APPBackgroundMode.m @@ -241,7 +241,7 @@ - (void) fireEvent:(NSString*)event */ + (NSString*) wkProperty { - NSString* str = @"X2Fsd2F5c1J1bnNBdEZvcmVncm91bmRQcmlvcml0eQ=="; + NSString* str = @"YWx3YXlzUnVuc0F0Rm9yZWdyb3VuZFByaW9yaXR5"; NSData* data = [[NSData alloc] initWithBase64EncodedString:str options:0]; return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; diff --git a/www/background-mode.js b/www/background-mode.js index fff3f8d3..3bfdf86a 100644 --- a/www/background-mode.js +++ b/www/background-mode.js @@ -408,16 +408,23 @@ exports._isActive = false; * * Default values of all available options. */ -exports._defaults = -{ - title: 'App is running in background', - text: 'Doing heavy tasks.', - bigText: false, - resume: true, - silent: false, - hidden: true, - color: undefined, - icon: 'icon' + +exports._defaults = { + title: 'App is running in background', + text: 'Doing heavy tasks.', + subText: '', + bigText: false, + resume: true, + silent: false, + hidden: true, + color: undefined, + icon: 'icon', + channelName: 'cordova-plugin-background-mode', + channelDescription: 'cordova-plugin-background-moden notification', + allowClose: false, + closeIcon: 'power', + closeTitle: 'Close', + showWhen: true }; /**