import {
BackgroundGeolocation,
BackgroundGeolocationConfig,
BackgroundGeolocationResponse
} from ‘@ionic-native/background-geolocation/ngx’;
import { LocalNotifications } from ‘@ionic-native/local-notifications/ngx’;
import { PowerManagement } from ‘@ionic-native/power-management/ngx’;
import {
BackgroundMode,
BackgroundModeConfiguration } from ‘@ionic-native/background-mode/ngx’;
watchCurrent() {
console.log(‘LiveMapService: watchUserCurrentPosition’);
// only background.
this.backgroundMode.enable();
this.backgroundMode.setDefaults(this.backgroundModeSetting);
this.powerManagement.dim().then(() => {
console.log('Wakelock acquired');
});
this.powerManagement.setReleaseOnPause(false).then(() => {
console.log('setReleaseOnPause successfully');
});
// app is running in background.
this.backgroundMode.on('activate').subscribe(() => {
this.backgroundGeolocation.start();
this.backgroundMode.disableWebViewOptimizations();
});
// app is running on foreground.
this.backgroundMode.on('deactivate').subscribe(() => {
this.backgroundGeolocation.stop();
});
// only foreground;
this.geolocation.watchPosition(this.geoConfig)
.pipe(
takeUntil(this.liveEnd$),
filter((position: Geoposition) => {
return !!(position as Geoposition).coords;
})
)
.subscribe(
(position: Geoposition) => {
this.zone.run(() => {
const location = position.coords;
this.currentGeoPointSubject.next({
lat: location.latitude,
lng: location.longitude,
timestamp: Date.now()
});
this.localNotifications.schedule({
text: 'Foreground Latitude ' + location.latitude + ' Longitude ' + location.longitude,
trigger: { at: new Date(new Date().getTime() + 5) },
led: 'FF0000',
sound: null
});
this.debugService.push([
`LiveMapService: watchUserCurrentPosition:`,
`lat: ${location.latitude}`,
`lng: ${location.longitude}`,
// `timestamp: ${Date.now()}`
]);
});
},
err => {
console.error(err);
this.debugService.push([
`LiveMapService: watchUserCurrentPosition: thrown Error.`,
`${err.message}`
]);
}
);
}