GithubHelp home page GithubHelp logo

cordova-plugin-advanced-background-mode's Introduction

Cordova Background Plugin

Maintenance npm version

Plugin for the Cordova framework to perform infinite background execution.

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 ope.githubn while in background, but does not deliver the data until the app resumes.

Donate

This and other Open-Source Cordova Plugins are developed in my free time. To help ensure this plugin is kept updated, new features are added and bugfixes are implemented quickly, please donate a couple of dollars (or a little more if you can stretch) as this will help me to afford to dedicate time to its maintenance. Please consider donating if you're using this plugin in an app that makes you money, if you're being paid to make the app, if you're asking for new features or priority bug fixes.

Table of Content

Original Plugin

This Plugin is a fork of this absolutely awesome Plugin by Katzer! Because it is unmainted, i decided to create this and keep it updated.

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.

Use the plugin by your own risk!

Supported Platforms

  • Android/Amazon FireOS
  • Browser
  • iOS
  • Windows (see #222)

Installation

The plugin can be installed via Cordova-CLI and is publicly available on NPM.

Execute from the projects root folder:

$ cordova plugin add cordova-plugin-advanced-background-mode

Usage

The plugin creates the object cordova.plugins.backgroundMode and is accessible after the deviceready event has been fired. It also includes an Ionic Wrapper

document.addEventListener('deviceready', function () {
    // cordova.plugins.backgroundMode is now available
}, false);

<!-- Ionic Wrapper -->
import BackgroundMode from 'cordova-plugin-advanced-background-mode';

Both Platforms

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.

cordova.plugins.backgroundMode.enable();
// or
cordova.plugins.backgroundMode.setEnabled(true);

To disable the background mode:

cordova.plugins.backgroundMode.disable();
// or
cordova.plugins.backgroundMode.setEnabled(false);

Check if running in background

Once the plugin has been enabled and the app has entered the background, the background mode becomes active.

cordova.plugins.backgroundMode.isActive(); // => boolean

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.

cordova.plugins.backgroundMode.on('EVENT', function);

To remove an event listeners:

cordova.plugins.backgroundMode.un('EVENT', function);

Android specifics

Transit between application states

Android allows to programmatically move from foreground to background or vice versa.

cordova.plugins.backgroundMode.moveToBackground();
// or
cordova.plugins.backgroundMode.moveToForeground();

Back button

Override the back button on Android to go to background instead of closing the app.

cordova.plugins.backgroundMode.overrideBackButton();

Exclude from Task list

Exclude the app from the recent task list (works on Android 5.0+).

cordova.plugins.backgroundMode.excludeFromTaskList();

Include to Task list

Include the app to the recent task list (works on Android 5.0+).

cordova.plugins.backgroundMode.includeToTaskList();

Detect screen status

The method works async instead of isActive() or isEnabled().

cordova.plugins.backgroundMode.isScreenOff((bool) => {
    ...
});

Unlock and wake-up

A wake-up turns on the screen while unlocking moves the app to foreground even the device is locked.

// Turn screen on
cordova.plugins.backgroundMode.wakeUp();
// Turn screen on and show app even locked
cordova.plugins.backgroundMode.unlock();

Notification

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.

Override defaults

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.

cordova.plugins.backgroundMode.setDefaults({
    title: String,
    text: String,
    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
})

To modify the currently displayed notification

cordova.plugins.backgroundMode.configure({ ... });

Note: All properties are optional - only override the things you need to.

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.

cordova.plugins.backgroundMode.setDefaults({ silent: true });

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.

cordova.plugins.backgroundMode.on('activate', function() {
   cordova.plugins.backgroundMode.disableWebViewOptimizations(); 
});

Note: Calling the method led to increased resource and power consumption.

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

Changelog

The full Changelog is available here.

cordova-plugin-advanced-background-mode's People

Contributors

chanhyuk-im avatar chpecson avatar christophersax-at avatar danielriera avatar dimitriscsd avatar einfachhans avatar hanskrywalsky avatar ihadeed avatar jerodfritz avatar katzer avatar mdailor avatar perrin4869 avatar pknittel avatar rtbm avatar siebmanb avatar soramister avatar steedems avatar thibaudd avatar tushe avatar woutwo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

cordova-plugin-advanced-background-mode's Issues

Issue with Apple CarPlay

Tested Background plugin with CarPlay.It is blocking streaming apps from playing via Apple CarPlay, which I think is a symptom of the larger background problem.
1.) In my car, when app is opened and I try to play music on my phone through another streaming service, the music from the streaming service bypasses the car system and directly plays through the iPhone speakers.
When I force quit app, the music starts playing through the car speaker system via CarPlay

Not working in iOS 15

I am using this plugin to keep playing audio while the app is running in background.

Which is working properly up to the device version below iOS 15.
But it is not working in iOS 15 devices.

Can anyone please help me to solve this issue?

dont exit application

The application can be closed in the task list when the application is in the background. How can I fix this?

App stopped - android

If the background mode is enabled then the app is getting closed when the app is minimized.
OS:android 10
Device :Xiomi device

ios build failed

Hi and thanks a lot for this fork. Unfortunately the ios release build fails.
Could you help please.

XCODE_VERSION=11.3
IOS_PLATFORM_VERSION=5.1.1

Failed to install 'cordova-plugin-advanced-background-mode': CordovaError: Uh oh! [21:34:42] File already exists at destination "/private/tmp/download/platforms/ios/That Shopping List/Resources/appbeep.wav" for resource file specified by plugin cordova-plugin-advanced-background-mode in iOS platform [21:34:42] at install (/private/tmp/download/platforms/ios/cordova/lib/plugman/pluginHandlers.js:64:23) [21:34:42] at ActionStack.process (/private/tmp/download/node_modules/cordova-common/src/ActionStack.js:56:25) [21:34:42] at PluginManager.doOperation (/private/tmp/download/node_modules/cordova-common/src/PluginManager.js:114:20) [21:34:42] at PluginManager.addPlugin (/private/tmp/download/node_modules/cordova-common/src/PluginManager.js:144:17) [21:34:42] at Api.addPlugin (/private/tmp/download/platforms/ios/cordova/Api.js:245:10) [21:34:42] at handleInstall (/Users/builder/n/lib/node_modules/cordova/node_modules/cordova-lib/src/plugman/install.js:581:10) [21:34:42] at /Users/builder/n/lib/node_modules/cordova/node_modules/cordova-lib/src/plugman/install.js:349:28 [21:34:42] at process._tickCallback (internal/process/next_tick.js:68:7) [21:34:42] Failed to restore plugin "cordova-plugin-advanced-background-mode" from config.xml. You might need to try adding it again. Error: Uh oh! [21:34:42] File already exists at destination "/private/tmp/download/platforms/ios/That Shopping List/Resources/appbeep.wav" for resource file specified by plugin cordova-plugin-advanced-background-mode in iOS platform

(bug):Plugin not working correctly on iOS 14

Bug Report

Current Behavior:

Plugin won't run a background task on iOS.
Expected Behavior:

The task consists of an infinite while loop that's suposed to have a 1 second pause and show the current time every 10 loops (equivalent to 10 seconds). It won't show anything in the console after the app is sent to background mode.
Steps to reproduce:

let cont=1
while (true) {
let now = new Date()
if(cont==10){
cont=1
// await this.pause(1)
console.log(now.getTime()+' '+now.toString())
}
else{
cont++
}
await this.pause(1000)
}

İonic 5 use error ?

Hello, can you help me?

I am using ionic 5 and angular .ı try background service app.
I can't add my project.

image

Not able to implement In ios.

HI I tried to implement this plugin for ios in ionic 5. I tried my best to call the methods but not working.
Steps I followed :

  1. Install the plugin
  2. Added in app module provider array
  3. in App component I tried to inject the and call the function

Can you provide some sample code? it will be very helpful. We r stuck for 15 days on background issues.

sorry for my bad English.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.