Need help upgrading to Ionic Framework 4.0? Get assistance with our Enterprise Migration Services EXPLORE NOW

Background Mode

Improve this doc

Cordova plugin to prevent the app from going to sleep while in background. Requires Cordova plugin: cordova-plugin-background-mode. For more info about plugin, visit: https://github.com/katzer/cordova-plugin-background-mode

Repo: https://github.com/katzer/cordova-plugin-background-mode

Installation

  1. Install the Cordova and Ionic Native plugins:
    $ ionic cordova plugin add cordova-plugin-background-mode
    $ npm install --save @ionic-native/background-mode@4
    
  2. Add this plugin to your app's module

Supported platforms

Usage

import { BackgroundMode } from '@ionic-native/background-mode';

constructor(private backgroundMode: BackgroundMode) { }

...

this.backgroundMode.enable();

Instance Members

enable()

Enable the background mode. Once called, prevents the app from being paused while in background.

disable()

Disable the background mode. Once the background mode has been disabled, the app will be paused when in background.

isEnabled()

Checks if background mode is enabled or not.

Returns: boolean returns a boolean that indicates if the background mode is enabled.

isActive()

Can be used to get the information if the background mode is active.

Returns: boolean returns a boolean that indicates if the background mode is active.

setDefaults(options)

Platforms:Android 

Override the default title, ticker and text. Available only for Android platform.

Param Type Details
options BackgroundModeConfiguration

List of option to configure. See table below

Returns: Promise<any>

configure(options)

Platforms:Android 

Modify the displayed information. Available only for Android platform.

Param Type Details
options BackgroundModeConfiguration

Any options you want to update. See table below.Optional

on(event)

Listen for events that the plugin fires. Available events are enable, disable, activate, deactivate and failure.

Param Type Details
event string

Event name

Returns: Observable<any>

moveToBackground()

Platforms:Android 

Android allows to programmatically move from foreground to background.

disableWebViewOptimizations()

Platforms:Android 

Enable GPS-tracking in background (Android).

moveToForeground()

Platforms:Android 

Android allows to programmatically move from background to foreground.

overrideBackButton()

Platforms:Android 

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

excludeFromTaskList()

Platforms:Android 

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

isScreenOff()

Platforms:Android 

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

Returns: Promise<boolean>

wakeUp()

Platforms:Android 

Turn screen on

unlock()

Platforms:Android 

Turn screen on and show app even locked

BackgroundModeConfiguration

Param Type Details
title string

Title of the background task

(optional)
text string

Description of background task

(optional)
icon string

This will look for <icon name>.png in platforms/android/res/drawable|mipmap

(optional)
color string

Set the background color of the notification circle

(optional)
resume boolean

By default the app will come to foreground when tapping on the notification. If false, plugin won't come to foreground when tapped.

(optional)
hidden boolean

When set to false makes the notifications visible on lock screen (Android 5.0+)

(optional)
bigText boolean

Big text

(optional)
ticker string

The text that scrolls itself on statusbar

(optional)
silent boolean

if true plugin will not display a notification. Default is false.

(optional)

API

Native

General