Background Task Plugin for Cordova

Brief overview

As part of a recent hackathon, myself and my teammate were struck by a 2am peril. We realised that we needed to run some JavaScript in our Cordova app for a very short period after the app was suspended and screen switched off.

Fortunately, I figured out a way before we lost too much time (or our minds). Today, I’m open sourcing the plugin for all to use.

Background task plugin

Allows you to run JavaScript when the app is active and for 3 minutes after the app is suspended.

Add the plugin with the CLI

Latest version:

1
cordova plugin add https://github.com/leecrossley/cordova-plugin-background-task.git

You do not need to reference any JavaScript, the Cordova plugin architecture will add a backgroundtask object to your root automatically when you build.

Usage

Start

It’s really simple, pass the JavaScript function you’d like to execute to backgroundtask.start() and your function will continue to run, even if the app is suspended for at least 3 minutes:

1
backgroundtask.start(functionToRun);

Future development

This plugin is for iOS only and was designed specifically for iOS 8. Issues and pull requests welcome on the git repo.