transistorsoft / react-native-background-geolocation

Sophisticated, battery-conscious background-geolocation with motion-detection
http://shop.transistorsoft.com/pages/react-native-background-geolocation
MIT License
2.62k stars 428 forks source link

Continuous Location Tracking in Background for Android and iOS #2116

Closed guneyural closed 3 weeks ago

guneyural commented 4 weeks ago

Your Environment

Expected Behavior

  1. How can I access location data received while the app is terminated? Because I need to do some calculations and store them in AsyncStorage. (the reason I don't use a server url is I have concerns about user's network connection. Because they might not have an internet connection during their walking sessions).
  2. Is there a solution for background tracking that allows for immediate location event processing (without detecting 200 meters of movement), similar to how .changePace(true) functions in the foreground?

Actual Behavior

This plugin works as expected on background and foreground. Since the movement of about 200 meters is required to start location service, I use .changePace(true). This way it immediately sending data to .onLocation listener and I do required calculations inside this listener then store them in redux. This is all working as expected on both platforms, on Foreground and when app is minimized (not terminated).

When I terminate the app on ios and android, around 200 meters of movement is required to activate the location service and then I receive notifications of moving state. But can't do calculations on received location data or store them to somewhere as I do in foreground using .onLocation method. Because I don't know what's the best way to access received background location data.

Steps to Reproduce

  1. You can create a new react native project and use the config above
  2. From iOS simulator select: Feature > Location > City run or (freeway drive, city run, city bicycle ride. As long as it simulates moving it's enough.)

Context

Since I'm working on a walking tracking application I want continous and accurate location tracking. I'm aware of battery optimizations such as waiting for around 200 meters of movement in the terminated state to start the location service but is there a hack like changePace(true) to immediately start location tracking in terminated state and store them locally to phone or somewhere suggested?

Debug logs

Logs ``` PASTE_YOUR_LOGS_HERE ```
christocracy commented 4 weeks ago

Because they might not have an internet connection during their walking sessions).

This plug-in was designed to operate offline. It was originally developed for a disaster response app, tracking emergency workers in earthquakes where the cellular network is assumed destroyed. That’s why the plug-in first inserts each recorded location into its own SQLite database, to be synced automatically when a network connection is restored. Read the api docs “HTTP Guide”, linked throughout.

Is there a solution for background tracking that allows for immediate location event processing (without detecting 200 meters of movement), similar to how .changePace(true) functions in the foreground?

No. We’re fortunate the OS even allows this.

christocracy commented 4 weeks ago

Also see api docs Config.enableHeadless

guneyural commented 4 weeks ago

After some research and your recommendations I decided to proceed with headlessTasks on android. I'm thinking of saving to redux or maybe AsyncStorage by assigning a background task on android.

For iOS, I thought about warning the users about I won't track their location if they terminate the app.

On iOS simulator, when I terminte the app by swiping up, after about 200-300 meters of movement location service re-opens again then tracks the user location (since debug: true I can see what's happening from notifications). Which I don't want this to happen.

My question is: How can I configure it to not to re-open location serivce even after a significant location change after app termination for ios only? (By terminating, I mean completely closing by swiping up)

How should I configure it to achieve that behavior? Do I need to call different .ready functions with different configs according to platform? I'm asking this because I have battery considerations. Since I won't be storing location data to anywhere after app termination on ios, I think there is no need to re-open the location service even after a significant location change.

My current config:

 BackgroundGeolocation.ready(
    {
      foregroundService: true,
      heartbeatInterval: 60,
      desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
      activityType: BackgroundGeolocation.ACTIVITY_TYPE_FITNESS,
      stopTimeout: 5,
      distanceFilter: 4,
      elasticityMultiplier: 0.9,
      debug: true,
      logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
      stopOnTerminate: false,
      startOnBoot: false,
      disableStopDetection: true,
      locationAuthorizationRequest: "Always",
      showsBackgroundLocationIndicator: true,
      allowIdenticalLocations: false,
      stopOnStationary: false,
      notification: {
        title: t("exercise"),
        text: t("yourExerciseSessionIsBeingRecorded"),
        priority: BackgroundGeolocation.NOTIFICATION_PRIORITY_MAX,
        color: "#4CAF50",
      },
    },
    (state) => {
      if (!state.enabled) {
        BackgroundGeolocation.start(function () {
          console.log("- Start success");

          BackgroundGeolocation.changePace(true);
        });
      }
    }
  );

As per the docs, on android, registerHeadlessTask in index.js will do all the job I want. I haven't tested yet on android but I think it'll work as expected so I didn't ask for android.

christocracy commented 4 weeks ago

What do you think this option does?

stopOnTerminate: false,

anandsharmaautovyn commented 4 weeks ago

@christocracy can i calculate the accurate distance using this library (client is cross checking it using google maps)?

christocracy commented 3 weeks ago

you are not replying then i suppose the package is not for me

Then I will close this issue.

anandsharmaautovyn commented 3 weeks ago

@christocracy can i calculate the accurate distance using this library (client is cross checking it using google maps)?

what about this question do you care to answer that?

christocracy commented 3 weeks ago

Search API docs odometer. The accuracy can be problematic since each recorded location introduces at least 5 meters of error. You will never get perfect accuracy calculating distance with geolocations.

anandsharmaautovyn commented 3 weeks ago

then what is the solution?

christocracy commented 3 weeks ago

then what is the solution?

Maybe there is no solution. I suggest you search the API docs "odometer" and test the results to see if they're good enough for you. If it's not good enough, then there is no solution.

anandsharmaautovyn commented 3 weeks ago

then what is the solution?

Maybe there is no solution. I suggest you search the API docs "odometer" and test the results to see if they're good enough for you. If it's not good enough, then there is no solution.

ok Thanks