Run function in background mode with time interval to execute http request and get alert popup


#1

Hello,

I’m trying to figure out, if it is possible, to run http request function with time interval and alert popup message window, from moment of Ionic2 apk installation on Android device and during background mode when application is closed.

For example if I use:

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

home.ts:

constructor(
    public platform: Platform,
    public backgroundMode: BackgroundMode
  ) { 
    platform.ready().then(() => {
      this.backgroundMode.on('activate').subscribe(() => {
        console.log('activated');
          this.showNotification();
      });
      this.backgroundMode.enable();
    })
  }

to do something like this:

  public background_function() {
  this.backgroundMode.enable();
  this.backgroundMode.on("activate").subscribe(() => {
      this.sub = Observable.interval(3000).subscribe((val) => {
        console.log('my http request function, executed every 3 seconds');
        alert("and popup, executed every 3 seconds");
      })
    });
    this.backgroundMode.enable();
  }

But seems like it does not works like this, not sure where and how I’ve to locate code to run function in background this way. Need help


#2

Even if this were possible, it’s a terrible idea. It will completely munch battery life into oblivion.


#3

@rapropos Hello, 3 seconds it is for example, in real application I need it for each hour with simple checking


#4

@rapropos Yes seem like it is impossible, then please check my new question, if it is possible to fix: