2013-10-08 08:28:55 +00:00
2017-01-26 13:54:32 +00:00
< p align = "left" >
< b > < a href = "https://github.com/katzer/cordova-plugin-background-mode/tree/example" > SAMPLE APP< / a > :point_right:< / b >
2014-11-17 13:06:54 +00:00
< / p >
2013-10-08 08:51:17 +00:00
2017-01-27 11:49:48 +00:00
Cordova Background Plugin [![npm version ](https://badge.fury.io/js/cordova-plugin-background-mode.svg )](http://badge.fury.io/js/cordova-plugin-background-mode) [![Build Status ](https://travis-ci.org/katzer/cordova-plugin-background-mode.svg?branch=master )](https://travis-ci.org/katzer/cordova-plugin-background-mode) [![codebeat badge ](https://codebeat.co/badges/49709283-b313-4ced-8630-f520baaec7b5 )](https://codebeat.co/projects/github-com-katzer-cordova-plugin-background-mode)
2017-01-26 13:54:32 +00:00
=========================
2013-10-08 08:51:17 +00:00
2017-01-26 13:54:32 +00:00
Plugin for the [Cordova][cordova] framework to perform infinite background execution.
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
2017-01-26 13:54:32 +00:00
#### Store Compliance
Infinite background tasks are not official supported on most mobile operation systems and thus not compliant with public store vendors. A successful submssion isn't garanteed.
2014-02-26 00:15:20 +00:00
2017-01-26 13:54:32 +00:00
Use the plugin by your own risk!
2014-02-13 13:15:12 +00:00
2016-02-29 08:55:07 +00:00
2017-01-26 13:54:32 +00:00
## Supported Platforms
2017-01-27 09:23:46 +00:00
- __Android/Amazon FireOS__
- __Browser__
2017-01-26 13:54:32 +00:00
- __iOS__
- __Windows__ _(see #222)_
2013-10-08 08:51:17 +00:00
2014-02-26 00:15:20 +00:00
2017-01-26 13:54:32 +00:00
## Installation
The plugin can be installed via [Cordova-CLI][CLI] and is publicly available on [NPM][npm].
2014-11-17 13:06:54 +00:00
2017-01-26 13:54:32 +00:00
Execute from the projects root folder:
2013-10-08 08:51:17 +00:00
2017-01-26 13:54:32 +00:00
$ cordova plugin add cordova-plugin-background-mode
2014-02-26 00:15:20 +00:00
2017-01-26 13:54:32 +00:00
Or install a specific version:
2014-11-17 13:06:54 +00:00
2017-01-26 13:54:32 +00:00
$ cordova plugin add de.appplant.cordova.plugin.background-mode@VERSION
2014-12-13 23:57:22 +00:00
2017-01-26 13:54:32 +00:00
Or install the latest head version:
2014-11-17 13:06:54 +00:00
2017-01-26 13:54:32 +00:00
$ cordova plugin add https://github.com/katzer/cordova-plugin-background-mode.git
Or install from local source:
2014-02-26 00:15:20 +00:00
2017-01-26 13:54:32 +00:00
$ cordova plugin add cordova-plugin-background-mode --searchpath < path >
__Note:__ In combination with the `cordova-plugin-wkwebview-engine` plugin the UIBackgroundModes section added by the plugin inside the *-Info.plist can be removed. That might increase the chance to submit the app to the app store.
2013-10-08 08:51:17 +00:00
2014-12-13 23:04:12 +00:00
## Usage
2017-01-26 13:54:32 +00:00
The plugin creates the object `cordova.plugins.backgroundMode` and is accessible after the *deviceready* event has been fired.
2014-11-17 13:06:54 +00:00
```javascript
document.addEventListener('deviceready', function () {
// cordova.plugins.backgroundMode is now available
}, false);
```
2017-01-26 13:54:32 +00:00
### Enable the background mode
The plugin is not enabled by default. Once it has been enabled the mode becomes active if the app moves to background.
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();
2017-01-26 13:54:32 +00:00
// or
cordova.plugins.backgroundMode.setEnabled(true);
2013-10-09 12:16:00 +00:00
```
2017-01-26 13:54:32 +00:00
To disable the background mode:
2013-10-09 12:16:00 +00:00
```javascript
2014-11-23 12:03:51 +00:00
cordova.plugins.backgroundMode.disable();
2017-01-26 13:54:32 +00:00
// or
cordova.plugins.backgroundMode.setEnabled(false);
2013-10-09 12:16:00 +00:00
```
2017-01-26 13:54:32 +00:00
__Note:__ By using `wkwebview-engine` on iOS, the plugin is enabled by default and cannot be disabled.
2014-12-14 13:23:57 +00:00
2017-01-26 13:54:32 +00:00
### Check if running in background
Once the plugin has been enabled and the app has entered the background, the background mode becomes active.
2014-12-14 13:23:57 +00:00
```javascript
cordova.plugins.backgroundMode.isActive(); // => boolean
2014-12-14 12:52:38 +00:00
```
2017-01-26 13:54:32 +00:00
A non-active mode means that the app is in foreground.
### Listen for events
The plugin fires an event each time its status has been changed. These events are `enable` , `disable` , `activate` , `deactivate` and `failure` .
2014-12-13 23:04:12 +00:00
```javascript
2017-01-26 13:54:32 +00:00
cordova.plugins.backgroundMode.on('EVENT', function);
2014-12-13 23:04:12 +00:00
```
2017-01-26 13:54:32 +00:00
To remove an event listeners:
2014-12-13 23:04:12 +00:00
```javascript
2017-01-26 13:54:32 +00:00
cordova.plugins.backgroundMode.un('EVENT', function);
2014-12-13 23:04:12 +00:00
```
2017-01-26 13:54:32 +00:00
## Android specifics
### Transit between application states
Android allows to programmatically move from foreground to background or vice versa.
2014-12-13 23:04:12 +00:00
```javascript
2017-01-26 13:54:32 +00:00
cordova.plugins.backgroundMode.moveToBackground();
// or
cordova.plugins.backgroundMode.moveToForeground();
2014-12-13 23:04:12 +00:00
```
2017-01-26 13:54:32 +00:00
### Back button
Override the back button on Android to go to background instead of closing the app.
2014-11-17 13:06:54 +00:00
```javascript
2017-01-26 13:54:32 +00:00
cordova.plugins.backgroundMode.overrideBackButton();
2014-11-17 13:06:54 +00:00
```
2017-01-29 10:16:53 +00:00
### Recent task list
Exclude the app from the recent task list works on Android 5.0+.
```javascript
cordova.plugins.backgroundMode.excludeFromTaskList();
```
2017-01-26 13:54:32 +00:00
### Notification
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
2017-01-26 13:54:32 +00:00
The title, 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 hidden to false will make the notification visible on lockscreen.
2014-11-17 13:06:54 +00:00
```javascript
2014-12-14 12:38:36 +00:00
cordova.plugins.backgroundMode.setDefaults({
2017-01-30 11:08:53 +00:00
title: String,
text: String,
2017-01-26 13:54:32 +00:00
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
2014-11-17 13:06:54 +00:00
})
```
2017-01-26 13:54:32 +00:00
To modify the currently displayed notification
2014-12-14 12:38:36 +00:00
```javascript
2017-01-26 13:54:32 +00:00
cordova.plugins.backgroundMode.configure({ ... });
2014-12-14 12:38:36 +00:00
```
2017-01-26 13:54:32 +00:00
__Note:__ All properties are optional - only override the things you need to.
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
2017-01-26 13:54:32 +00:00
cordova.plugins.backgroundMode.configure({ silent: true });
2015-01-01 16:24:37 +00:00
```
2014-11-17 13:06:54 +00:00
2017-01-26 13:54:32 +00:00
## Quirks
Various APIs like playing media or tracking GPS position in background might not work while in background even the background mode is active. To fix such issues the plugin provides a method to disable most optimizations done by Android/CrossWalk.
```javascript
2017-01-27 23:15:53 +00:00
cordova.plugins.backgroundMode.on('activate', function() {
2017-01-26 13:54:32 +00:00
cordova.plugins.backgroundMode.disableWebViewOptimizations();
});
```
__Note:__ Calling the method led to increased resource and power consumption.
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-26 13:54:32 +00:00
Made with :yum: from Leipzig
© 2017 [appPlant GmbH][appplant]
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
2017-01-26 13:54:32 +00:00
[NPM]: ???
2014-11-17 13:06:54 +00:00
[changelog]: CHANGELOG.md
[apache2_license]: http://opensource.org/licenses/Apache-2.0
[appplant]: http://appplant.de