1

I am building an app using Ionic which is supposed to run in the background and as soon as an event of interest happens, the app should appear in the foreground.

To do that I use BackgroundMode.

In the constructor I enable background mode.

this.backgroundMode.enable();

In order to test this functionality, I crated a function which after pressing the button, waits for 5 seconds and sends a local notification and supposed to bring the application to foreground

  bringToForeground() {
    this.sleep(5000).then(() => {
      this.backgroundMode.moveToForeground();
      this.localNotifications.schedule({
        id: 2,
        text: '2 -> Single ILocalNotification',
        sound: 'file://sound.mp3',
        data: { secret: "" }
      });

    })

  }


  sleep(time) {
    return new Promise((resolve) => setTimeout(resolve, time));
  }

To test it, I

  1. Run the application on Samsung Galaxy 7 (Android)
  2. Press a button in the application to trigger the function
  3. Press home button on the phone so that the app goes to the background
  4. Wait.

What happens is that I see the notification, the app is still running in the background.

Any idea about what is going on?

Alex
  • 1,054
  • 6
  • 25
  • 42

1 Answers1

0

You have used the enabled() on constructor? Something like this:

startBackgroundMode(){

        console.log('Iniciando background mode')        

        this.backgroundMode.enable();
        this.backgroundMode.overrideBackButton();               
        //this.backgroundMode.excludeFromTaskList();    

        this.backgroundMode.on('activate').subscribe(value => {
            this.backgroundMode.disableWebViewOptimizations();
        });
    }

For me, the solution was use an Observable, then I subscribe and call moveToForeGround()

Check the complete answer here

Diego Desenvolvedor
  • 378
  • 1
  • 6
  • 22