Unable to provide geolocation when app in background mode


Hi Everyone,

I want to make ionic app to run in the background and get geolocation.

Basic installation as follows.

  1. nodejs 8.11.3 (npm v5.6.0)
  2. Cordova 8.0.0
  3. Ionic 3.20.0

Other plugins:

npm install --save @ionic-native/core@latest

ionic cordova plugin add cordova-plugin-geolocation
npm install --save @ionic-native/geolocation

ionic plugin add cordova-plugin-mauron85-background-geolocation
npm install --save @ionic-native/background-geolocation

ionic cordova plugin add cordova-plugin-statusbar
npm install --save @ionic-native/status-bar

ionic cordova plugin add cordova-plugin-inappbrowser
npm install --save @ionic-native/in-app-browser

Provider: LocationTracker


import { Injectable, NgZone } from '@angular/core';
import { BackgroundGeolocation } from '@ionic-native/background-geolocation';
import { Geolocation, Geoposition } from '@ionic-native/geolocation';
import 'rxjs/add/operator/filter';
export class LocationTracker {
  public watch: any;   
  constructor(public zone: NgZone, 
    public backgroundGeolocation: BackgroundGeolocation, 
    public geolocation: Geolocation) {

  startTracking() {
    // Background Tracking
    let config = {
      desiredAccuracy: 0,
      stationaryRadius: 20,
      distanceFilter: 10,
      debug: false,
      interval: 60000
    this.backgroundGeolocation.configure(config).subscribe((location) => {
      //console.log('BackgroundGeolocation:  ' + location.latitude + ',' + location.longitude);
      // Run update inside of Angular's zone
      this.zone.run(() => {
        this.lat = location.latitude;
        this.lng = location.longitude;
    }, (err) => {
    // Turn ON the background-geolocation system.
    // Foreground Tracking
    let options = {
      frequency: 3000,
      enableHighAccuracy: true

    this.watch = this.geolocation.watchPosition(options).filter((p: any) => p.code === undefined).subscribe((position: Geoposition) => {
      // Run update inside of Angular's zone
      this.zone.run(() => {
        this.lat = position.coords.latitude;
        this.lng = position.coords.longitude;

  stopTracking() {


import { BrowserModule } from '@angular/platform-browser';
import { NgModule, ErrorHandler } from '@angular/core';
import { IonicApp, IonicModule, IonicErrorHandler } from 'ionic-angular';
import { MyApp } from './app.component';
import { HomePage } from '../pages/home/home';
import { Geolocation } from '@ionic-native/geolocation';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { HttpClientModule } from '@angular/common/http';
import { BackgroundGeolocation } from '@ionic-native/background-geolocation';
import { LocationTracker } from '../providers/location-tracker/location-tracker';

  declarations: [
  imports: [
    IonicModule.forRoot(MyApp, { animate: false })
  bootstrap: [IonicApp],
  entryComponents: [
  providers: [
    {provide: ErrorHandler, useClass: IonicErrorHandler},
export class AppModule {}


import { Component} from '@angular/core';
import { NavController,Platform } from 'ionic-angular';
import { Geolocation } from '@ionic-native/geolocation';
import { HttpClient } from '@angular/common/http';
import { LocationTracker } from '../../providers/location-tracker/location-tracker';

	selector: 'home-page',
  templateUrl: 'home.html'

export class HomePage {

  constructor(public navCtrl: NavController, 
    public geolocation: Geolocation, 
    public http: HttpClient, 
    public locationTracker: LocationTracker,
    public rest: Rest,
    public platform: Platform) {
      this.platform.ready().then(() => {


Application is running fine on foreground mode, but it’s unable to generate geolocation in background mode.

Is there anything wrong in the code? How can I fix it?


P.S. I am running this app on Android device ( Nougat )

How to start second thread in ionic

Unfortunately for your users, even if you did manage to achieve these three goals, you would also get #4: utterly demolish battery life. I would urge you to rethink this so that your app is not preventing the device from sleeping.


Thanks for the reply @rapropos.

Please provide me with a better approach to achieve the existing thing.



At a minimum, I would pool all data collected during background operation and only do the network uploading when the app is in the foreground.


OK. That’s a good idea, but I am building a tracking app. So I have another app where I want to show the movement of this device in the google map. If the app goes background mode for 10 min, then the movement will also stop for 10 min.


This all sounds like one of the creepiest ideas I’ve ever heard.


From your reply I can understand that there are much better ideas. Some other concept which I am lacking due to my limited knowledge. May be I can achieve in some other way. Something like getting geolocation for device from google itself? Is it possible?


I am really sorry wasting your time. I should have researched more before come to here. I found Hypertrack is the thing which I wanted.

I appreciate the time you have given. Thanks @rapropos


Can anyone shed some light on the issue?

Thanks and regards.


Hello Jerry,

Do you find any solution ?



I installed

and add the code for wake_lock

Anyone tried @mauron85/cordova-plugin-background-geolocation?