npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

cordova-plugin-background-mode-xjp

v0.7.3

Published

Prevent apps from going to sleep in background.

Downloads

2

Readme

This is a fork of a great plugin by katzer. It aims to keep up-to-date with Android changes while also providing more features.

Cordova Background Plugin npm version Build Status codebeat badge

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

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 will be publicly available on NPM eventually.

Execute from the projects root folder:

$ cordova plugin add https://bitbucket.org/TheBosZ/cordova-plugin-run-in-background

Usage

The plugin creates the object cordova.plugins.backgroundMode and is accessible after the deviceready event has been fired.

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

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.

Note: starting with Android 10, you must request the "Draw on Top" permission from the user or the call to moveToForeground will silently fail. You can request it with cordova.plugins.backgroundMode.requestForegroundPermission();. This permission isn't necessary for moveToBackground

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();

Recent task list

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

cordova.plugins.backgroundMode.excludeFromTaskList();

Detect screen status

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

cordova.plugins.backgroundMode.isScreenOff(function(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();

Request to disable battery optimizations

Starting in Android 8, apps can be put to sleep to conserve battery. When this happens (usually after 5 minutes or so), the background task is killed. This will cause things like MQTT connections to break.

This method will show a permission prompt for the user (only if the app hasn't been granted permission) to ignore the optimization.

cordova.plugins.backgroundMode.disableBatteryOptimizations();

You can also open the battery optimization settings menu directly, and get the user to set it manually. This may be a better option for devices which may ignore the prompt above.

cordova.plugins.backgroundMode.openBatteryOptimizationsSettings();

To check if battery optimizations are disabled for the app:

cordova.plugins.backgroundMode.isIgnoringBatteryOptimizations(function(isIgnoring) {
    ...
})

Additionally, you may find that your JS code begins to run less frequently, or not at all while in the background. This can be due to the webview slowing down its execution due to being in the background. The disableWebViewOptimizations function can prevent that, but it's important that it is run after the app goes to the background.

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

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,
    subText: String, // see https://developer.android.com/reference/android/support/v4/app/NotificationCompat.Builder.html#setSubText(java.lang.CharSequence)
    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,
    channelName: String, // Shown when the user views the app's notification settings
    channelDescription: String, // Shown when the user views the channel's settings
    allowClose: Boolean, // add a "Close" action to the notification
    closeIcon: 'power', // An icon shown for the close action
    closeTitle: 'Close', // The text for the close action
    showWhen: Boolean, //(Default: true) Show the time since the notification was created
    visibility: String, // Android only: one of 'private' (default), 'public' or 'secret' (see https://developer.android.com/reference/android/app/Notification.Builder.html#setVisibility(int))
})

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

License

This software is released under the Apache 2.0 License.

Made with :yum: from Leipzig

? 2017 appPlant GmbH & meshfields