You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

README.md 6.7 KiB

4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. <p align="left">
  2. <b><a href="https://github.com/katzer/cordova-plugin-background-mode/tree/example">SAMPLE APP</a> :point_right:</b>
  3. </p>
  4. 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)
  5. =========================
  6. Plugin for the [Cordova][cordova] framework to perform infinite background execution.
  7. 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.
  8. The system keeps all network connections open while in background, but does not deliver the data until the app resumes.
  9. #### Store Compliance
  10. 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.
  11. Use the plugin by your own risk!
  12. ## Supported Platforms
  13. - __Android/Amazon FireOS__
  14. - __Browser__
  15. - __iOS__
  16. - __Windows__ _(see #222)_
  17. ## Installation
  18. The plugin can be installed via [Cordova-CLI][CLI] and is publicly available on [NPM][npm].
  19. Execute from the projects root folder:
  20. $ cordova plugin add cordova-plugin-background-mode
  21. Or install a specific version:
  22. $ cordova plugin add de.appplant.cordova.plugin.background-mode@VERSION
  23. Or install the latest head version:
  24. $ cordova plugin add https://github.com/katzer/cordova-plugin-background-mode.git
  25. Or install from local source:
  26. $ cordova plugin add cordova-plugin-background-mode --searchpath <path>
  27. ## Usage
  28. The plugin creates the object `cordova.plugins.backgroundMode` and is accessible after the *deviceready* event has been fired.
  29. ```js
  30. document.addEventListener('deviceready', function () {
  31. // cordova.plugins.backgroundMode is now available
  32. }, false);
  33. ```
  34. ### Enable the background mode
  35. The plugin is not enabled by default. Once it has been enabled the mode becomes active if the app moves to background.
  36. ```js
  37. cordova.plugins.backgroundMode.enable();
  38. // or
  39. cordova.plugins.backgroundMode.setEnabled(true);
  40. ```
  41. To disable the background mode:
  42. ```js
  43. cordova.plugins.backgroundMode.disable();
  44. // or
  45. cordova.plugins.backgroundMode.setEnabled(false);
  46. ```
  47. ### Check if running in background
  48. Once the plugin has been enabled and the app has entered the background, the background mode becomes active.
  49. ```js
  50. cordova.plugins.backgroundMode.isActive(); // => boolean
  51. ```
  52. A non-active mode means that the app is in foreground.
  53. ### Listen for events
  54. The plugin fires an event each time its status has been changed. These events are `enable`, `disable`, `activate`, `deactivate` and `failure`.
  55. ```js
  56. cordova.plugins.backgroundMode.on('EVENT', function);
  57. ```
  58. To remove an event listeners:
  59. ```js
  60. cordova.plugins.backgroundMode.un('EVENT', function);
  61. ```
  62. ## Android specifics
  63. ### Transit between application states
  64. Android allows to programmatically move from foreground to background or vice versa.
  65. ```js
  66. cordova.plugins.backgroundMode.moveToBackground();
  67. // or
  68. cordova.plugins.backgroundMode.moveToForeground();
  69. ```
  70. ### Back button
  71. Override the back button on Android to go to background instead of closing the app.
  72. ```js
  73. cordova.plugins.backgroundMode.overrideBackButton();
  74. ```
  75. ### Recent task list
  76. Exclude the app from the recent task list works on Android 5.0+.
  77. ```js
  78. cordova.plugins.backgroundMode.excludeFromTaskList();
  79. ```
  80. ### Detect screen status
  81. The method works async instead of _isActive()_ or _isEnabled()_.
  82. ```js
  83. cordova.plugins.backgroundMode.isScreenOff(function(bool) {
  84. ...
  85. });
  86. ```
  87. ### Unlock and wake-up
  88. A wake-up turns on the screen while unlocking moves the app to foreground even the device is locked.
  89. ```js
  90. // Turn screen on
  91. cordova.plugins.backgroundMode.wakeUp();
  92. // Turn screen on and show app even locked
  93. cordova.plugins.backgroundMode.unlock();
  94. ```
  95. ### Notification
  96. 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.
  97. #### Override defaults
  98. 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.
  99. ```js
  100. cordova.plugins.backgroundMode.setDefaults({
  101. title: String,
  102. text: String,
  103. icon: 'icon' // this will look for icon.png in platforms/android/res/drawable|mipmap
  104. color: String // hex format like 'F14F4D'
  105. resume: Boolean,
  106. hidden: Boolean,
  107. bigText: Boolean
  108. })
  109. ```
  110. To modify the currently displayed notification
  111. ```js
  112. cordova.plugins.backgroundMode.configure({ ... });
  113. ```
  114. __Note:__ All properties are optional - only override the things you need to.
  115. #### Run in background without notification
  116. 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.
  117. ```js
  118. cordova.plugins.backgroundMode.setDefaults({ silent: true });
  119. ```
  120. ## Quirks
  121. 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.
  122. ```js
  123. cordova.plugins.backgroundMode.on('activate', function() {
  124. cordova.plugins.backgroundMode.disableWebViewOptimizations();
  125. });
  126. ```
  127. __Note:__ Calling the method led to increased resource and power consumption.
  128. ## Contributing
  129. 1. Fork it
  130. 2. Create your feature branch (`git checkout -b my-new-feature`)
  131. 3. Commit your changes (`git commit -am 'Add some feature'`)
  132. 4. Push to the branch (`git push origin my-new-feature`)
  133. 5. Create new Pull Request
  134. ## License
  135. This software is released under the [Apache 2.0 License][apache2_license].
  136. Made with :yum: from Leipzig
  137. ? 2017 [appPlant GmbH][appplant] & [meshfields][meshfields]
  138. [cordova]: https://cordova.apache.org
  139. [CLI]: http://cordova.apache.org/docs/en/edge/guide_cli_index.md.html#The%20Command-line%20Interface
  140. [NPM]: ???
  141. [changelog]: CHANGELOG.md
  142. [apache2_license]: http://opensource.org/licenses/Apache-2.0
  143. [appplant]: http://appplant.de
  144. [meshfields]: http://meshfields.de