seididieci / capacitor-backround-geolocation

Ionic capacitor backgroung geolocation Plugin
MIT License
12 stars 2 forks source link

ionic vue background geolocation tracking stops after 5 minute #17

Closed ramay2088 closed 3 years ago

ramay2088 commented 3 years ago

Describe the bug I am using plugin
https://github.com/seididieci/capacitor-backround-geolocation to watch the user's location. then I am tracking that location with the help of a pusher. this background location works only for 5 minutes after that it just stops. I am using Capacitor's Background task. But that plugin also keeps data on phone after the user opens the app. the Background task sends data to the pusher.

Here is watch location function

  getLocation: async function () {
      BackgroundGeolocation.initialize({
        notificationText: "Your app is running, tap to open.",
        notificationTitle: "App Running",
        updateInterval: 10000,
        requestedAccuracy: BgGeolocationAccuracy.HIGH_ACCURACY,
        // Small icon has to be in 'drawable' resources of your app
        // if you does not provide one (or it is not found) a fallback icon will be used.
        smallIcon: "ic_small_icon",
        // Start getting location updates right away. You can set this to false or not set at all (se below).
        startImmediately: true,
      });
      // const geolocation = new Geolocation.Geolocation();
      BackgroundGeolocation.addListener("onLocation", (location) => {
        // console.log("Got new location", location);
        this.subscribe(location.longitude, location.latitude);
        console.log(location)
      });

      BackgroundGeolocation.addListener("onPermissions", (location) => {
        // console.log("BGLocation permissions:", location);
        this.subscribe(location.longitude, location.latitude);

        // Do something with data
      });
      BackgroundGeolocation.start();
    },

Then calling function in mounted()

mounted(){
this.getLocation
  App.addListener("appStateChange", (state) => {
       setInterval(this.getLocation, 120000);
      if (!state.isActive) {
        BackgroundTask.beforeExit(async () => {
          setInterval(this.getLocation, 120000);
          console.og('Why')
        });
      }
      if (state.isActive) {
        setInterval(this.getLocation, 120000);
        console.log('Active')
      }
    });
}

To Reproduce Steps to reproduce the behavior: just go in background it will work only for 5 minutes

Expected behavior It should keep send location on server

Screenshots If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information): Android 10

ramay2088 commented 3 years ago

well well well! here is solution Android 8+ don't allow continue watch user's location for longer period of time in order to do that we need to create foreground service.

https://ionicframework.com/docs/native/foreground-service

seididieci commented 3 years ago

I updated the documentation so it is more explicit.