Skip to content

ionic 4 when app minimize it not work #30

@Jawwad-Parekh

Description

@Jawwad-Parekh

i try it but it giving issue i started it on click but it not working in background? Like this

startBackgroundGeolocation() {
const config: BackgroundGeolocationConfig = {
desiredAccuracy: 10,
stationaryRadius: 1,
distanceFilter: 1,
interval:15000,
debug: true, // enable this hear sounds for background-geolocation life-cycle.
stopOnTerminate: false // enable this to clear background location settings when the app terminates

};

this.backgroundGeolocation.configure(config).then(() => {
  this.backgroundGeolocation
    .on(BackgroundGeolocationEvents.location)
    .subscribe((location: BackgroundGeolocationResponse) => {
      // alert(location.longitude);
      this.sendGPS(location);

      // 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.
    });
});

// start recording location
this.backgroundGeolocation.start();

// If you wish to turn OFF background-tracking, call the #stop method.
// this.backgroundGeolocation.stop();

}

sendGPS(location) {
if (location.speed == undefined) {
location.speed = 0;
}
let timestamp = new Date(location.time);

this.http
  .post(
    this.gps_update_link, // backend api to post
    {
      lat: location.latitude,
      lng: location.longitude,
      speed: location.speed,
      timestamp: timestamp
    },
    {}
  )
  .then(data => {
    this.Data.push({lat : location.latitude , lon : location.longitude , spe : location.speed , time : location.timestamp});
    console.log(data.status);
    console.log(data.data); // data received by server
    console.log(data.headers);
    this.backgroundGeolocation.finish(); // FOR IOS ONLY
  })
  .catch(error => {
    alert(error.status);
    alert(error.error); // error message as string
    alert(error.headers);
    this.backgroundGeolocation.finish(); // FOR IOS ONLY
  });

}

This is a code i am using please inform me what i am doing wrong?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions