Cordova plugin to prevent the app from going to sleep while in background.
Most mobile operating systems are multitasking capable, but most apps dont need to run while in background and not present for the user. Therefore they pause the app in background mode and resume the app before switching to foreground mode. The system keeps all network connections open while in background, but does not deliver the data until the app resumes.
This cordova plug-in can be used for applications, who rely on continuous network communication independent of from direct user interactions and remote push notifications.
The plugin focuses on enterprise-only distribution and may not compliant with all public store vendors.
- iOS (including iOS8)
- Android (SDK >=11)
- WP8
The plugin can either be installed from git repository, from local file system through the Command-line Interface. Or cloud based through PhoneGap Build.
From master:
# ~~ from master branch ~~
cordova plugin add https://github.com/katzer/cordova-plugin-background-mode.git
from a local folder:
# ~~ local folder ~~
cordova plugin add de.appplant.cordova.plugin.background-mode --searchpath path
or to use the last stable version:
# ~~ stable version ~~
cordova plugin add [email protected]
To remove the plug-in, run the following command:
cordova plugin rm de.appplant.cordova.plugin.background-mode
Add the following xml to your config.xml to always use the latest version of this plugin:
<gap:plugin name="de.appplant.cordova.plugin.background-mode" version="0.6.2" />
More informations can be found here.
- [bugfix:] Type error
- [bugfix:] Wrong default values for
isEnabled
andisActive
.
- [enhancement:] Set default settings through
setDefaults
. - [enhancement:] New method
isEnabled
to receive if mode is enabled. - [enhancement:] New method
isActive
to receive if mode is active. - [bugfix:] Events caused thread collision.
- The former
plugin.backgroundMode
namespace has been deprecated and will be removed with the next major release. - See CHANGELOG.md to get the full changelog for the plugin.
- Plug-in is broken on Windows Phone 8.1 platform.
The plugin creates the object cordova.plugins.backgroundMode
with the following methods:
- backgroundMode.enable
- backgroundMode.disable
- backgroundMode.isEnabled
- backgroundMode.isActive
- backgroundMode.getDefaults
- backgroundMode.setDefaults
- backgroundMode.configure
- backgroundMode.onactivate
- backgroundMode.ondeactivate
- backgroundMode.onfailure
The plugin and its methods are not available before the deviceready event has been fired.
document.addEventListener('deviceready', function () {
// cordova.plugins.backgroundMode is now available
}, false);
To prevent the app from being paused while in background, the backroundMode.enable
interface has to be called.
- The background mode will be activated once the app has entered the background and will be deactivated after the app has entered the foreground.
- To activate the background mode the app needs to be in foreground.
cordova.plugins.backgroundMode.enable();
The background mode can be disabled through the backgroundMode.disable
interface.
- Once the background mode has been disabled, the app will be paused when in background.
cordova.plugins.backgroundMode.disable();
The backgroundMode.isEnabled
interface can be used to get the information if the background mode is enabled or disabled.
cordova.plugins.backgroundMode.isEnabled(); // => boolean
The backgroundMode.isActive
interface can be used to get the information if the background mode is active.
cordova.plugins.backgroundMode.isActive(); // => boolean
The backgroundMode.onactivate
interface can be used to get notified when the background mode has been activated.
cordova.plugins.backgroundMode.onactivate = function() {};
The backgroundMode.ondeactivate
interface can be used to get notified when the background mode has been deactivated.
- Once the mode has been deactivated the app will be paused soon after the callback has been fired.
cordova.plugins.backgroundMode.ondeactivate = function() {};
The backgroundMode.onfailure
interface can be used to get notified when the background mode could not been activated.
The listener has to be a function and takes the following arguments:
- errorCode: Error code which describes the error
cordova.plugins.backgroundMode.onfailure = function(errorCode) {};
The following example demonstrates how to enable the background mode after device is ready. The mode itself will be activated when the app has entered the background.
document.addEventListener('deviceready', function () {
// Android customization
cordova.plugins.backgroundMode.setDefaults({ text:'Doing heavy tasks.'});
// Enable background mode
cordova.plugins.backgroundMode.enable();
// Called when background mode has been activated
cordova.plugins.backgroundMode.onactivate = function () {
setTimeout(function () {
// Modify the currently displayed notification
cordova.plugins.backgroundMode.configure({
text:'Running in background for more than 5s now.'
});
}, 5000);
}
}, false);
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.
The title, ticker and text for that notification can be customized as follows:
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.
cordova.plugins.backgroundMode.setDefaults({
resume: false
})
It's also possible to modify the currently displayed notification while in background.
cordova.plugins.backgroundMode.configure({
title: String,
...
})
- Fork it
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request
This software is released under the Apache 2.0 License.
© 2013-2014 appPlant UG, Inc. All rights reserved