capacitor-community / background-geolocation

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

background location stops broadcasting after five minutes #53

Closed ramay2088 closed 2 years ago

ramay2088 commented 2 years ago

Describe the bug I am trying to share currrent location of user through pusher it works fine when app is open but when i minimize the app. after 5 minutes . it stops working . but the thing is I can see location get change when app is in background through devtool console. App remains alive when it is minimize . Foreground service is also running but still can't share location .. When app is minimize it seems like all the locations are added in queue. when i open app it start sending all queue locations . I also tried https request instead of pusher that also stops after 5 minutes here is the code.

   BackgroundGeolocation.addWatcher(
    {
        // If the "backgroundMessage" option is defined, the watcher will
        // provide location updates whether the app is in the background or the
        // foreground. If it is not defined, location updates are only
        // guaranteed in the foreground. This is true on both platforms.

        // On Android, a notification must be shown to continue receiving
        // location updates in the background. This option specifies the text of
        // that notification.
        backgroundMessage: "Requerido para el rastreo en tiempo real",

        // The title of the notification mentioned above. Defaults to "Using
        // your location".
        backgroundTitle: "Rastreo Siloc",

        // Whether permissions should be requested from the user automatically,
        // if they are not already granted. Defaults to "true".
        requestPermissions: true,

        // If "true", stale locations may be delivered while the device
        // obtains a GPS fix. You are responsible for checking the "time"
        // property. If "false", locations are guaranteed to be up to date.
        // Defaults to "false".
        stale: false,
        // The minimum number of metres between subsequent locations. Defaults
        // to 0.
        distanceFilter: 0
    },
    (location, error)=> {
        if (error) {
            if (error.code === "NOT_AUTHORIZED") {
                if (window.confirm(
                    "Esta app necesita acceso a tu ubicación" +
                    "para poder mostrar las funciones necesarias al llegar a la huerta y/o báscula\n\n" +
                    "¿Abrir ajustes ahora?"
                )) {
                    // It can be useful to direct the user to their device's
                    // settings when location permissions have been denied. The
                    // plugin provides the 'openSettings' method to do exactly
                    // this.
                    BackgroundGeolocation.openSettings();
                }
            }
            return console.error(error);
        }

   // console.log("Got new location", location);
        this.subscribe(location.longitude, location.latitude);
        let user = TokenService.getUserInfo();
        const work_order_id = TokenService.getWorkOrder();
        if (work_order_id != null) {
          let work_order = JSON.parse(
            this.$localStorage.get(`work_order_${work_order_id}`)
          );
          if (user != null && work_order?.status == 8) {
            // delivery reached
            var delivery_longitude = 0;
            var delivery_latitude = 0;
            if (work_order?.fruit_delivery_location == "Empaque") {
              delivery_latitude = Number(
                work_order?.packaging_company_details?.packaging_companies
                  ?.latitude
              );
              delivery_longitude = Number(
                work_order?.packaging_company_details?.packaging_companies
                  ?.longitude
              );
            } else {
              delivery_latitude = Number(work_order?.delivery_latitude);
              delivery_longitude = Number(work_order?.delivery_longitude);
            }

            var distance = this.checkDistance(
              delivery_latitude,
              delivery_longitude,
              location.latitude,
              location.longitude,
              "K"
            );

            if (distance <= 1) {
              if (work_order.status == 8) {
                this.markOrderComplete(work_order.id);
                this.downloadWorkOrders();
              }
            }
            //delivery reached
          }
        }

        return 0;
    }
).then(function after_the_watcher_has_been_added() {

    // When a watcher is no longer needed, it should be removed by calling
    // 'removeWatcher' with an object containing its ID.
    // BackgroundGeolocation.removeWatcher({
    //     id: watcher_id
    // });
});

To Reproduce install package then minimize the app

Expected behavior it should share the location when app is close. I checked that pusher is also remain connected . when app is minmize .

Desktop (please complete the following information): windows 11

Smartphone (please complete the following information): android 10,9

Additional context I also tried to disable battery optimize and and webview.. but still no success

diachedelic commented 2 years ago

I think this is a duplicate of #14. The Android webview imposes limitations on network requests when the app is in the background.