Try this:
import {
BackgroundGeolocation ,
BackgroundGeolocationConfig ,
BackgroundGeolocationResponse ,
BackgroundGeolocationLocationProvider,
BackgroundGeolocationEvents
} from '@ionic-native/background-geolocation';
...
let config: BackgroundGeolocationConfig = {
desiredAccuracy: 10,
stationaryRadius: 1,
distanceFilter: 1,
debug: true,
interval: 2000,
stopOnTerminate: false,
url: 'my-link', // location is sent automatically to this url in background or foreground or app is closed
postTemplate: {
lat: '@latitude',
lng: '@longitude'
};
this.backgroundGeolocation.configure(config).then((location: BackgroundGeolocationResponse)=>{
this.globals.lat = location.latitude;
this.globals.lng = location.longitude;
}, (error) => {
alert(error);
});
this.backgroundGeolocation.on(BackgroundGeolocationEvents['location']).subscribe((location) => {
// here put code for the action you want after location changed event
// this code does not work when app is closed
});
This works in foreground and background!