user9088454
user9088454

Reputation: 1122

Run function when background mode is active using ionic 4

I am using the ionic Background Mode plugin. First, I installed in the project, imported in the app.module.ts file and put this code in app.component.ts file this.backgroundMode.enable();. I want to check if background mode is active in the background run function. I want to run my function when the background mode is active.

let inBackground = true;

this.backgroundMode.isActive();

this.myfunction();

Does anyone know how to do this??

Upvotes: 2

Views: 5336

Answers (2)

Chawki Messaoudi
Chawki Messaoudi

Reputation: 762

For example

import { BackgroundMode } from '@ionic-native/background-mode';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/interval';

And in your constructor

constructor(public platform: Platform, public backgroundMode: BackgroundMode) {
    this.platform.ready().then(() => {
        this.backgroundMode.on('activate').subscribe(() => {
            // Call your method here
        });

        this.backgroundMode.enable();
    });
}

Upvotes: 0

Karpov Vladimir
Karpov Vladimir

Reputation: 150

Better to do it as in example below:

this.backgroundMode.on('activate').subscribe(s => {
        console.log('backgroundMode activate');
 });
 this.backgroundMode.enable();

More information about it you can take from this Cordova Background Plugin

Upvotes: 4

Related Questions