mirror of
https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background
synced 2024-11-22 07:14:54 +00:00
JS interface to customize the notification on Android
This commit is contained in:
parent
96ae5df2ca
commit
439330a1d4
@ -21,6 +21,7 @@
|
|||||||
|
|
||||||
<!-- js -->
|
<!-- js -->
|
||||||
<js-module src="www/background-mode.js" name="BackgroundMode">
|
<js-module src="www/background-mode.js" name="BackgroundMode">
|
||||||
|
<clobbers target="cordova.plugins.backgroundMode" />
|
||||||
<clobbers target="plugin.backgroundMode" />
|
<clobbers target="plugin.backgroundMode" />
|
||||||
</js-module>
|
</js-module>
|
||||||
|
|
||||||
|
@ -33,6 +33,7 @@ import org.apache.cordova.CallbackContext;
|
|||||||
import org.apache.cordova.CordovaPlugin;
|
import org.apache.cordova.CordovaPlugin;
|
||||||
import org.json.JSONArray;
|
import org.json.JSONArray;
|
||||||
import org.json.JSONException;
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
public class BackgroundMode extends CordovaPlugin {
|
public class BackgroundMode extends CordovaPlugin {
|
||||||
|
|
||||||
@ -42,6 +43,9 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
// Flag indicates if the plugin is enabled or disabled
|
// Flag indicates if the plugin is enabled or disabled
|
||||||
private boolean isDisabled = false;
|
private boolean isDisabled = false;
|
||||||
|
|
||||||
|
// Settings for the notification
|
||||||
|
static JSONObject settings;
|
||||||
|
|
||||||
// Used to (un)bind the service to with the activity
|
// Used to (un)bind the service to with the activity
|
||||||
private ServiceConnection connection = new ServiceConnection() {
|
private ServiceConnection connection = new ServiceConnection() {
|
||||||
|
|
||||||
@ -80,6 +84,11 @@ public class BackgroundMode extends CordovaPlugin {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (action.equalsIgnoreCase("configure")) {
|
||||||
|
settings = args.getJSONObject(0);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
if (action.equalsIgnoreCase("enable")) {
|
if (action.equalsIgnoreCase("enable")) {
|
||||||
enableMode();
|
enableMode();
|
||||||
return true;
|
return true;
|
||||||
|
@ -32,6 +32,9 @@ import android.os.Handler;
|
|||||||
import android.os.IBinder;
|
import android.os.IBinder;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
|
import org.json.JSONException;
|
||||||
|
import org.json.JSONObject;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Timer;
|
import java.util.Timer;
|
||||||
import java.util.TimerTask;
|
import java.util.TimerTask;
|
||||||
@ -126,15 +129,18 @@ public class ForegroundService extends Service {
|
|||||||
PendingIntent contentIntent = PendingIntent.getActivity(
|
PendingIntent contentIntent = PendingIntent.getActivity(
|
||||||
context, NOTIFICATION_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
|
context, NOTIFICATION_ID, intent, PendingIntent.FLAG_CANCEL_CURRENT);
|
||||||
|
|
||||||
String title = "App is running in background";
|
JSONObject settings = BackgroundMode.settings;
|
||||||
|
|
||||||
Notification.Builder notification = new Notification.Builder(context)
|
Notification.Builder notification = new Notification.Builder(context)
|
||||||
.setContentTitle(title)
|
.setContentTitle(settings.optString("title"))
|
||||||
.setContentText(title)
|
.setContentText(settings.optString("text"))
|
||||||
.setTicker(title)
|
.setTicker(settings.optString("ticker"))
|
||||||
.setOngoing(true)
|
.setOngoing(true)
|
||||||
.setSmallIcon(getIconResId())
|
.setSmallIcon(getIconResId());
|
||||||
.setContentIntent(contentIntent);
|
|
||||||
|
if (settings.optBoolean("resume")) {
|
||||||
|
notification.setContentIntent(contentIntent);
|
||||||
|
}
|
||||||
|
|
||||||
if (Build.VERSION.SDK_INT < 16) {
|
if (Build.VERSION.SDK_INT < 16) {
|
||||||
// Build notification for HoneyComb to ICS
|
// Build notification for HoneyComb to ICS
|
||||||
|
@ -19,33 +19,86 @@
|
|||||||
under the License.
|
under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
var BackgroundMode = function () {
|
var exec = require('cordova/exec'),
|
||||||
|
channel = require('cordova/channel');
|
||||||
|
|
||||||
|
|
||||||
|
// Override back button action to prevent being killed
|
||||||
|
document.addEventListener('backbutton', function () {}, false);
|
||||||
|
|
||||||
|
channel.deviceready.subscribe( function () {
|
||||||
// Registriert die Listener für die (sleep/resume) Events
|
// Registriert die Listener für die (sleep/resume) Events
|
||||||
cordova.exec(null, null, 'BackgroundMode', 'observeLifeCycle', []);
|
cordova.exec(null, null, 'BackgroundMode', 'observeLifeCycle', []);
|
||||||
|
|
||||||
|
exports.configure();
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* List of all available options with their default value.
|
||||||
|
*
|
||||||
|
* @return {Object}
|
||||||
|
*/
|
||||||
|
exports.getDefaults = function () {
|
||||||
|
return {
|
||||||
|
title: 'App is running in background',
|
||||||
|
text: 'Doing heavy tasks.',
|
||||||
|
ticker: 'App is running in background',
|
||||||
|
resume: true
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
BackgroundMode.prototype = {
|
/**
|
||||||
/**
|
* Activates the background mode. When activated the application
|
||||||
* @public
|
* will be prevented from going to sleep while in background
|
||||||
*
|
* for the next time.
|
||||||
* Aktiviert den Hintergrundmodus.
|
*/
|
||||||
*/
|
exports.enable = function () {
|
||||||
enable: function () {
|
cordova.exec(null, null, 'BackgroundMode', 'enable', []);
|
||||||
cordova.exec(null, null, 'BackgroundMode', 'enable', []);
|
};
|
||||||
},
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @public
|
* Deactivates the background mode. When deactivated the application
|
||||||
*
|
* will not stay awake while in background.
|
||||||
* Deaktiviert den Hintergrundmodus
|
*/
|
||||||
*/
|
exports.disable = function () {
|
||||||
disable: function () {
|
cordova.exec(null, null, 'BackgroundMode', 'disable', []);
|
||||||
cordova.exec(null, null, 'BackgroundMode', 'disable', []);
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Configures the notification settings for Android.
|
||||||
|
* Will be merged with the defaults.
|
||||||
|
*
|
||||||
|
* @param {Object} options
|
||||||
|
* Dict with key/value pairs
|
||||||
|
*/
|
||||||
|
exports.configure = function (options) {
|
||||||
|
var settings = this.mergeWithDefaults(options || {});
|
||||||
|
|
||||||
|
cordova.exec(null, null, 'BackgroundMode', 'configure', [settings]);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @private
|
||||||
|
*
|
||||||
|
* Merge settings with default values.
|
||||||
|
*
|
||||||
|
* @param {Object} options
|
||||||
|
* The custom options
|
||||||
|
*
|
||||||
|
* @return {Object}
|
||||||
|
* Default values merged
|
||||||
|
* with custom values
|
||||||
|
*/
|
||||||
|
exports.mergeWithDefaults = function (options) {
|
||||||
|
var defaults = this.getDefaults();
|
||||||
|
|
||||||
|
for (var key in defaults) {
|
||||||
|
if (!options.hasOwnProperty(key)) {
|
||||||
|
options[key] = defaults[key];
|
||||||
|
continue;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return options;
|
||||||
};
|
};
|
||||||
|
|
||||||
var plugin = new BackgroundMode();
|
|
||||||
|
|
||||||
document.addEventListener("backbutton", function () {}, false);
|
|
||||||
|
|
||||||
module.exports = plugin;
|
|
Loading…
Reference in New Issue
Block a user