I am working on an Ionic app. I am trying to send location in background every 1 hour. I am using official Background Geolocation plugin. Location is being sent when the app is open either in foreground or background but as soon as i close the app my location is not being sent to database. Although my debug mode is active and i can see toast message and beep sound even after closing app. Can anyone point out where i am making mistake? Here is my code.
{
platform.ready().then(() => {
this.getLocation();
this.backgroundGeolocation.start();
});
}
getLocation() {
const config: BackgroundGeolocationConfig = {
locationProvider: this.backgroundGeolocation.LocationProvider.ANDROID_DISTANCE_FILTER_PROVIDER,
desiredAccuracy: 10,
stationaryRadius: 5,
startOnBoot: true,
notificationIconColor: '#4CAF50',
interval: 5000,
fastestInterval: 5000,
activitiesInterval: 5000,
distanceFilter: 5,
debug: true, // enable this hear sounds for background-geolocation life-cycle.
stopOnTerminate: false, // enable this to clear background location settings when the app terminates
stopOnStillActivity: false
};
this.backgroundGeolocation.configure(config).subscribe((location) => {
this.loc.push(location);
this.storage.save('Location', this.loc).subscribe();
console.log('Location :', location);
this.sendLocation(location);
});
}
sendLocation(data) {
const url = 'my-url';
const payload = {
date_added: moment(data.time).format('DD/MM/YYYY'),
identity: '',
ipaddress: '',
latitude: data.latitude,
longitude: data.longitude,
mobile: 'phoneNo',
network_name: '',
status: '',
time_added: moment(data.time).format('hh:mm A'),
}
this.http.post(url, payload).map(res => res.json()).subscribe(data => console.log('location inserted: ', data))
}