capacitor-community / background-geolocation

A Capacitor plugin that sends you geolocation updates, even while the app is in the background.
MIT License
177 stars 54 forks source link

http request fail in background mode #82

Closed ikkettuvvoi closed 1 year ago

ikkettuvvoi commented 1 year ago

I connected the plugin to my application in Ionic 6.

I noticed that the locations on my web server are not arriving in the background after a few minutes.

By doing a thorough investigation I have agreed that the position is processed correctly in the backgound but the HTTP (POST) call is no longer performed

To Reproduce

async configureBackgroundGeolocation() {
    const uuid = await Device.getId();

    BackgroundGeolocation.addWatcher(
      {
        backgroundMessage: "Track attivo in background",
        backgroundTitle: "Track attivo.",
        requestPermissions: true,
        stale: false,
        distanceFilter: 1
      },
      (location, error) => {
        if (error) {
          if (error.code === "NOT_AUTHORIZED") {
            if (window.confirm(
              "This app needs your location, " +
              "but does not have permission.\n\n" +
              "Open settings now?"
            )) {
              BackgroundGeolocation.openSettings();
            }
          }
          return console.error(error);
        }

        console.log('gillo gps: ', location);

        try {
          let url = environment.api + "/mappa/gps";
          this.http.post(url, { debug: 0, location: location, seriale: uuid }).subscribe((res: any) => {
            console.log('gillo post gps', res);
          },
            (error) => {
              console.log("gillo error (**) ", error)
            });
        } catch (err) {
          console.log("gillo error (*) ", err)
        }
        return location;
      }
    ).then(function after_the_watcher_has_been_added(watcher_id) {

    });
  }
ikkettuvvoi commented 1 year ago

I apologize, I forgot in a hurry to say thank you.

diachedelic commented 1 year ago

See #14.

diachedelic commented 1 year ago

In short, you must use Capacitor's HTTP plugin to send HTTP requests in the background.

ikkettuvvoi commented 1 year ago

perfect! thanks!