cordova-plugin-background-geolocation
cordova-plugin-background-geolocation copied to clipboard
The plugin only takes the geolocation once
Your Environment
Plugin version: cordova-plugin-background-geolocation 3.1.0 "CDVBackgroundGeolocation" Platform: Android OS version: 10 Device manufacturer and model: XIAOMI REDMI NOTE 9 PRO
- Running in Simulator:
Cordova version (
cordova -v
): 10.0.0 Cordova platform version (cordova platform ls
): Installed platforms: android 8.1.0
Available platforms: browser ^6.0.0
electron ^1.0.0
windows ^7.0.0 - Plugin configuration options:
- Link to your project:
Context
This is my first time using this library and I dont know why but this doest work properly. This is my code:
`import { Injectable } from '@angular/core';
import { BackgroundGeolocation, BackgroundGeolocationConfig, BackgroundGeolocationEvents, BackgroundGeolocationResponse } from '@ionic-native/background-geolocation/ngx';
import { LocalNotifications } from '@ionic-native/local-notifications/ngx';
declare var window;
@Injectable({
providedIn: 'root'
})
export class BackgroundGPSService {
config: BackgroundGeolocationConfig = {
desiredAccuracy: 0,
stationaryRadius: 10,
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
};
locations:any;
contador=0;
constructor( private backgroundGeolocation: BackgroundGeolocation, private localNotifications: LocalNotifications) {
this.locations=[];
this.backgroundGeolocation.configure(this.config).then(() => {
this.backgroundGeolocation.on(BackgroundGeolocationEvents.location).subscribe((location: BackgroundGeolocationResponse) => {
console.log('Locations', location);
console.log('Speed', location.speed); // Tracks the speed of user
this.contador ++;
this.showNotification(this.contador+" "+location.latitude+" "+location.longitude)
});
});
}
StartBackgroundTracking(){
this.backgroundGeolocation.start();
}
StopBackgroundGeolocation(){
this.backgroundGeolocation.stop();
}
showNotification(data){
// Schedule a single notification
this.localNotifications.schedule({
id: 1,
text: JSON.stringify(data),
sound: 'file://sound.mp3',
data: { secret: "key" }
});
}
/**Hasta aqui */
}
Im testing the app in a xiaomi redmi note 9 pro.
Expected Behavior
What I want is to recieve a notificaction with a counter + latitude+ longitude each time that I change of location.
Actual Behavior
The problem is that this doesnt work properly.
When I lauch this.backgroundGeolocation.start();
this only takes the location once. After this u can see the notification with the first latitude and longitude, but this is all.
This never changes, except sometimes that this show 2 location points when u launch it (the two proint are an aproximation from where u are , and the second is where u are).
Possible Fix
Steps to Reproduce
1.I install the plugin and I add :
import { BackgroundGeolocation} from '@ionic-native/background-geolocation/ngx';
NgModule({
declarations: [AppComponent],
entryComponents: [],
imports: [BrowserModule, IonicModule.forRoot(), AppRoutingModule],
providers: [
...
BackgroundGeolocation,
...
{ provide: RouteReuseStrategy, useClass: IonicRouteStrategy }
],
bootstrap: [AppComponent]
})
export class AppModule {}
to app.module.ts 2. I create a services called background-gpsservice.service.ts and I add:
import { Injectable } from '@angular/core';
import { BackgroundGeolocation, BackgroundGeolocationConfig, BackgroundGeolocationEvents, BackgroundGeolocationResponse } from '@ionic-native/background-geolocation/ngx';
import { LocalNotifications } from '@ionic-native/local-notifications/ngx';
declare var window;
@Injectable({
providedIn: 'root'
})
export class BackgroundGPSService {
config: BackgroundGeolocationConfig = {
desiredAccuracy: 0,
stationaryRadius: 10,
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
};
locations:any;
contador=0;
constructor( private backgroundGeolocation: BackgroundGeolocation, private localNotifications: LocalNotifications) {
this.locations=[];
this.backgroundGeolocation.configure(this.config).then(() => {
this.backgroundGeolocation.on(BackgroundGeolocationEvents.location).subscribe((location: BackgroundGeolocationResponse) => {
console.log('Locations', location);
console.log('Speed', location.speed); // Tracks the speed of user
this.contador ++;
this.showNotification(this.contador+" "+location.latitude+" "+location.longitude)
// IMPORTANT: You must execute the finish method here to inform the native plugin that you're finished,
// and the background-task may be completed. You must do this regardless if your operations are successful or not.
// IF YOU DON'T, ios will CRASH YOUR APP for spending too much time in the background.
// this.backgroundGeolocation.finish(); // FOR IOS ONLY
});
});
}
StartBackgroundTracking(){
this.backgroundGeolocation.start();
}
StopBackgroundGeolocation(){
this.backgroundGeolocation.stop();
}
showNotification(data){
// Schedule a single notification
this.localNotifications.schedule({
id: 1,
text: JSON.stringify(data),
sound: 'file://sound.mp3',
data: { secret: "key" }
});
}
}
-
After this I add a new tab to my app with two buttons calling the methods
StartBackgroundTracking()
andStopBackgroundGeolocation()
from the servicebackground-gpsservice.service
that I created before. -
The app show the notification with "background traking enables" and few second after this, shows the notification with the counter+latitude+longitude.
-
In theory it should continue showing the new coordinates when Im moving , but this doesnt happends.
Context
This is really a big problem because I need a app with background tracking, and this give me the position only once.
Debug logs
I have the same issue on devices Android 10 and Android 11; and in both of Emulators.
I even tried other forks claiming keep working in background but no success. For example this one mentioned on this project: 749
I have tha same problem, just in android, in iOS seems to be working fine. I keep looking for a solution for this issue. Any of you made it work?