Ionic program dont work in backgraund. How to fix that?

96 Views Asked by At

I need the app to run fully in the background and when I use other apps on my phone. The program transmits video from the camera. When I turn off the screen, I managed to get the program to transmit video, but when I turn on other programs on the phone, the program stops transmitting. Part of the code as I managed to omit in off mode. i use library @ionic-native/background-mode

  componentDidMount() {
    document.addEventListener('deviceready', () => {
      BackgroundMode.setEnabled(true);
      BackgroundMode.disableBatteryOptimizations();
      BatteryStatus.onChange().subscribe(status => {
        this.batteryStatus = status;
        this.signalCurrentStatus();
      });
    },false);
1

There are 1 best solutions below

0
On

Use plugin Background Mode

ionic cordova plugin add cordova-plugin-background-mode

npm install @ionic-native/background-mode

Then use it like this:

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

export class AppComponent {
    constructor(private backgroundMode: BackgroundMode) {
        this.initializeApp();
    }

    initializeApp() {
        this.platform.ready().then(() => {
            this.backgroundMode.enable();
        });
    }
}