2016-02-29 08:58:39 +00:00
[![npm version ](https://badge.fury.io/js/cordova-plugin-background-mode.svg )](http://badge.fury.io/js/cordova-plugin-background-mode)
2013-10-08 08:28:55 +00:00
2014-11-17 13:06:54 +00:00
< p align = "right" >
< a href = "https://github.com/katzer/cordova-plugin-background-mode/tree/example" > EXAMPLE :point_right:< / a >
< / p >
2013-10-08 08:51:17 +00:00
2014-11-17 13:06:54 +00:00
Cordova Background Plug-in
==========================
2013-10-08 08:51:17 +00:00
2014-11-17 13:06:54 +00:00
[Cordova][cordova] plugin to prevent the app from going to sleep while in background.
2014-02-26 00:15:20 +00:00
2014-11-17 13:06:54 +00:00
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.
2013-10-08 08:51:17 +00:00
2014-11-17 13:06:54 +00:00
### Plugin's Purpose
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.
2014-02-26 00:15:20 +00:00
2014-11-17 13:06:54 +00:00
### :bangbang: Store Compliance :bangbang:
The plugin focuses on enterprise-only distribution and may not compliant with all public store vendors.
2014-02-13 13:15:12 +00:00
2016-02-29 08:55:07 +00:00
__Update:__ The plugin ID has changed to cordova-plugin-background-mode and is available under npm. An updated version comes later!
2013-10-08 08:51:17 +00:00
2014-11-17 13:06:54 +00:00
## Overview
1. [Supported Platforms ](#supported-platforms )
2. [Installation ](#installation )
3. [ChangeLog ](#changelog )
2014-12-13 23:04:12 +00:00
4. [Usage ](#usage )
2014-11-17 13:06:54 +00:00
5. [Examples ](#examples )
6. [Platform specifics ](#platform-specifics )
2014-02-26 00:15:20 +00:00
2014-11-17 13:06:54 +00:00
## Supported Platforms
2014-12-14 00:13:58 +00:00
- __iOS__ (_including iOS8_)
2014-12-13 23:20:43 +00:00
- __Android__ _(SDK >=11)_
2014-11-17 13:06:54 +00:00
- __WP8__
2015-11-25 16:24:11 +00:00
- __Windows 10__
2013-10-08 08:51:17 +00:00
2014-02-26 00:15:20 +00:00
2014-11-17 13:06:54 +00:00
## Installation
The plugin can either be installed from git repository, from local file system through the [Command-line Interface][CLI]. Or cloud based through [PhoneGap Build][PGB].
### Local development environment
From master:
```bash
# ~~ from master branch ~~
cordova plugin add https://github.com/katzer/cordova-plugin-background-mode.git
2014-02-26 00:15:20 +00:00
```
2014-11-17 13:06:54 +00:00
from a local folder:
```bash
# ~~ local folder ~~
cordova plugin add de.appplant.cordova.plugin.background-mode --searchpath path
2014-02-26 00:15:20 +00:00
```
2014-11-17 13:06:54 +00:00
or to use the last stable version:
```bash
# ~~ stable version ~~
2015-01-01 17:33:45 +00:00
cordova plugin add de.appplant.cordova.plugin.background-mode@0.6.3
2014-12-13 23:57:22 +00:00
```
To remove the plug-in, run the following command:
```bash
cordova plugin rm de.appplant.cordova.plugin.background-mode
2014-02-26 00:15:20 +00:00
```
2014-11-17 13:06:54 +00:00
### PhoneGap Build
Add the following xml to your config.xml to always use the latest version of this plugin:
```xml
2015-01-01 17:33:45 +00:00
< gap:plugin name = "de.appplant.cordova.plugin.background-mode" version = "0.6.3" / >
2014-02-26 00:15:20 +00:00
```
2014-11-17 13:06:54 +00:00
More informations can be found [here][PGB_plugin].
2014-02-13 13:22:21 +00:00
2013-12-01 13:06:04 +00:00
2014-11-17 13:06:54 +00:00
## ChangeLog
2017-01-18 15:52:23 +00:00
#### Version 0.7.0 (not yet released)
- __Features__
2017-01-23 09:08:05 +00:00
- Support for Amazon FireOS
2017-01-18 15:52:23 +00:00
- Ability to configure icon and color on Android
- Allow app to move to foreground on Android
- Allow app to move to background on Android
- Allow app to override back button behaviour on Android
- New events for when the mode has been enabled/disabled
- __Improvements__
- Various enhancements and bug fixes for all platforms
- Support for latest platform and OS versions
- Multi line text on Android
- Multiple listeners for same event
- Compatibility with cordova-plugin-geolocation
- Compatibility with cordova-plugin-crosswalk-webview
- Compatibility with cordova-plugin-wkwebview-engine
- New sample app
- __Fixes__
- Silent mode issues on Android
- Lock screen issues on Android
- Callback not called on Android
- Notification shows app info with cordova-android@6
- Other apps audio interruption on iOS
- __Changes__
- Deprecate event callbacks
- Notification not visible anymore on lock screen
- Remove unexpected back button handler
- Remove support for wp8 platform
2014-12-14 14:41:04 +00:00
2014-11-17 13:06:54 +00:00
#### Further informations
- See [CHANGELOG.md][changelog] to get the full changelog for the plugin.
2013-10-09 12:16:00 +00:00
2014-12-13 23:04:12 +00:00
#### Known issues
2017-01-18 15:52:23 +00:00
- README is out of date !!!
2013-10-08 08:51:17 +00:00
2014-12-13 23:04:12 +00:00
## Usage
2014-12-14 13:23:57 +00:00
The plugin creates the object `cordova.plugins.backgroundMode` with the following methods:
2013-10-09 12:16:00 +00:00
2014-11-17 13:06:54 +00:00
1. [backgroundMode.enable][enable]
2. [backgroundMode.disable][disable]
2014-12-14 12:52:38 +00:00
3. [backgroundMode.isEnabled][is_enabled]
2014-12-14 13:23:57 +00:00
4. [backgroundMode.isActive][is_active]
5. [backgroundMode.getDefaults][android_specifics]
6. [backgroundMode.setDefaults][android_specifics]
2014-12-14 13:29:10 +00:00
7. [backgroundMode.configure][configure]
2014-12-14 13:23:57 +00:00
8. [backgroundMode.onactivate][onactivate]
9. [backgroundMode.ondeactivate][ondeactivate]
10. [backgroundMode.onfailure][onfailure]
2013-10-09 12:16:00 +00:00
2014-11-17 13:06:54 +00:00
### Plugin initialization
The plugin and its methods are not available before the *deviceready* event has been fired.
```javascript
document.addEventListener('deviceready', function () {
// cordova.plugins.backgroundMode is now available
}, false);
```
### Prevent the app from going to sleep in background
2015-08-30 12:59:46 +00:00
To prevent the app from being paused while in background, the `backgroundMode.enable` interface has to be called.
2014-11-17 13:06:54 +00:00
#### Further informations
- 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.
2013-10-08 14:32:10 +00:00
2013-10-09 12:16:00 +00:00
```javascript
2014-11-23 12:03:51 +00:00
cordova.plugins.backgroundMode.enable();
2013-10-09 12:16:00 +00:00
```
2014-11-17 13:06:54 +00:00
### Pause the app while in background
The background mode can be disabled through the `backgroundMode.disable` interface.
#### Further informations
- Once the background mode has been disabled, the app will be paused when in background.
2013-10-09 12:16:00 +00:00
```javascript
2014-11-23 12:03:51 +00:00
cordova.plugins.backgroundMode.disable();
2013-10-09 12:16:00 +00:00
```
2014-12-14 12:52:38 +00:00
### Receive if the background mode is enabled
2014-12-14 13:23:57 +00:00
The `backgroundMode.isEnabled` interface can be used to get the information if the background mode is enabled or disabled.
2014-12-14 12:52:38 +00:00
```javascript
2014-12-14 13:23:57 +00:00
cordova.plugins.backgroundMode.isEnabled(); // => boolean
```
2015-11-25 16:24:11 +00:00
### Receive if the background mode is active (Android/iOS only)
2014-12-14 13:23:57 +00:00
The `backgroundMode.isActive` interface can be used to get the information if the background mode is active.
```javascript
cordova.plugins.backgroundMode.isActive(); // => boolean
2014-12-14 12:52:38 +00:00
```
2015-11-25 16:24:11 +00:00
### Get informed when the background mode has been activated (Android/iOS only)
2014-12-13 23:04:12 +00:00
The `backgroundMode.onactivate` interface can be used to get notified when the background mode has been activated.
```javascript
cordova.plugins.backgroundMode.onactivate = function() {};
```
2015-11-25 16:24:11 +00:00
### Get informed when the background mode has been deactivated (Android/iOS only)
2014-12-13 23:04:12 +00:00
The `backgroundMode.ondeactivate` interface can be used to get notified when the background mode has been deactivated.
#### Further informations
- Once the mode has been deactivated the app will be paused soon after the callback has been fired.
```javascript
cordova.plugins.backgroundMode.ondeactivate = function() {};
```
### Get informed when the background mode could not been activated
2015-11-25 16:24:11 +00:00
The `backgroundMode.onfailure` interface can be used to get notified when the background mode could not be activated (Android/iOS) or enabled (Windows 10).
2014-12-13 23:04:12 +00:00
The listener has to be a function and takes the following arguments:
- errorCode: Error code which describes the error
```javascript
cordova.plugins.backgroundMode.onfailure = function(errorCode) {};
```
2014-11-17 13:06:54 +00:00
## Examples
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.
```javascript
document.addEventListener('deviceready', function () {
// Android customization
2014-12-14 12:38:36 +00:00
cordova.plugins.backgroundMode.setDefaults({ text:'Doing heavy tasks.'});
2014-12-13 23:04:12 +00:00
// Enable background mode
cordova.plugins.backgroundMode.enable();
2014-12-14 12:38:36 +00:00
2014-12-13 23:04:12 +00:00
// Called when background mode has been activated
cordova.plugins.backgroundMode.onactivate = function () {
2014-12-14 12:38:36 +00:00
setTimeout(function () {
// Modify the currently displayed notification
cordova.plugins.backgroundMode.configure({
text:'Running in background for more than 5s now.'
});
}, 5000);
2014-12-13 23:04:12 +00:00
}
2014-11-17 13:06:54 +00:00
}, false);
```
2013-12-01 12:49:36 +00:00
## Platform specifics
2013-10-10 10:38:32 +00:00
2014-12-14 12:38:36 +00:00
### Android customization
2014-12-14 13:29:10 +00:00
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.
2013-10-10 10:38:32 +00:00
2014-12-14 12:38:36 +00:00
#### Override defaults
2015-06-04 09:59:11 +00:00
The title, ticker, text and icon 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.
2015-06-03 15:33:09 +00:00
All of these fields are optional - only override the things you need to.
2014-11-17 13:06:54 +00:00
```javascript
2014-12-14 12:38:36 +00:00
cordova.plugins.backgroundMode.setDefaults({
2014-11-17 13:06:54 +00:00
title: String,
ticker: String,
2015-06-03 15:33:09 +00:00
text: String,
2015-06-04 09:59:11 +00:00
icon: "icon" // this will look for icon.png in platforms/android/res/drawable
2015-06-03 15:33:09 +00:00
resume: true / false,
color: "#123456",
2015-06-04 09:59:11 +00:00
isPublic: true / false,
2014-11-17 13:06:54 +00:00
})
```
2014-12-14 12:38:36 +00:00
#### Modify the currently displayed notification
It's also possible to modify the currently displayed notification while in background.
```javascript
cordova.plugins.backgroundMode.configure({
title: String,
...
})
```
2015-01-01 16:24:37 +00:00
#### Run in background without notification
In silent mode the plugin will not display a notification - which is not the default. Be aware that Android recommends adding a notification otherwise the OS may pause the app.
```javascript
cordova.plugins.backgroundMode.configure({
silent: true
})
```
2014-11-17 13:06:54 +00:00
2013-12-01 12:57:36 +00:00
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request
2013-12-01 13:06:04 +00:00
2014-11-17 13:06:54 +00:00
2013-12-01 13:06:04 +00:00
## License
2014-11-17 13:06:54 +00:00
This software is released under the [Apache 2.0 License][apache2_license].
2017-01-01 21:40:41 +00:00
© 2013-2017 appPlant GmbH, Inc. All rights reserved
2014-11-17 13:06:54 +00:00
[cordova]: https://cordova.apache.org
[CLI]: http://cordova.apache.org/docs/en/edge/guide_cli_index.md.html#The%20Command-line%20Interface
[PGB]: http://docs.build.phonegap.com/en_US/index.html
2015-03-05 22:53:15 +00:00
[PGB_plugin]: https://build.phonegap.com/plugins/2056
2014-11-17 13:06:54 +00:00
[changelog]: CHANGELOG.md
2014-12-14 13:27:14 +00:00
[enable]: #prevent -the-app-from-going-to-sleep-in-background
[disable]: #pause -the-app-while-in-background
[is_enabled]: #receive -if-the-background-mode-is-enabled
[is_active]: #receive -if-the-background-mode-is-active
[android_specifics]: #android -customization
2014-12-14 13:29:10 +00:00
[configure]: #modify -the-currently-displayed-notification
2014-12-14 13:27:14 +00:00
[onactivate]: #get -informed-when-the-background-mode-has-been-activated
[ondeactivate]: #get -informed-when-the-background-mode-has-been-deactivated
[onfailure]: #get -informed-when-the-background-mode-could-not-been-activated
2014-11-17 13:06:54 +00:00
[apache2_license]: http://opensource.org/licenses/Apache-2.0
[appplant]: http://appplant.de