2

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??

user9088454
  • 1,076
  • 1
  • 15
  • 45

2 Answers2

4

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

Karpov Vladimir
  • 140
  • 1
  • 1
  • 9
0

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();
    });
}
Chawki Messaoudi
  • 714
  • 2
  • 6
  • 18