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?

0 Answers