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.63k stars 428 forks source link

Not getting data in killed moving state on iOS on any real-device. #1232

Closed Reenagrg100 closed 3 years ago

Reenagrg100 commented 3 years ago

Your Environment

Plugin version: react-native-background-geolocation": "3.9.3" & react-native-background-fetch": "3.0.6" Platform: iOS only OS version: 13.1.1,14.3 Device manufacturer / model: iPhone Xr and iPhone 12mini

React Native version (react-native -v): 0.62.2


export const configureBackgroundGeoLocation = () => {
  BackgroundGeolocation.ready(
    {
      desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
      distanceFilter: stores.locationStore.fetchRadius || 10, //distance required for getting location update        // Activity Recognition
      stopTimeout: 5, //this is a timer which runs when plugin eneter into it's stationary state
      disableStopDetection: false, // this is not recommended because will NEVER disable location service until we call background geo location stop

      // Application config
      debug: false, // <-- enable this hear sounds for background-geolocation life-cycle.
      logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
      stopOnTerminate: false, // <-- Allow the background-service to continue tracking when user closes the app and required for Headless JS ( android killing state)

      startOnBoot: true, // <-- Auto start tracking when device is powered-up.
      locationAuthorizationRequest: 'Always',
      preventSuspend: true, //required for heartbeat event for ios,keep your iOS app running indefinitely in the background and prevent the app to going to suspend mode

      //android options
      foregroundService: true,
      // With Headless JS, you can continue to respond to BackgroundGeolocation events after your android app has been terminated (assuming you've configured stopOnTerminate: false). but there is no headless mode in iOS
      enableHeadless: true,
    },
    state => {
      LOGGER.log(
        '- BackgroundGeolocation is configured and ready: ',
        state.enabled,
      );

      if (!state.enabled) {
        BackgroundGeolocation.start(function() {
          LOGGER.log('- Start success');
        });
      }
    },
  );

  /**
   *1. calls a function whenever there is an update in location(update depends on distanceFilter=radius(coming from server))
   *2. will be always get called for at least one time, even if there is no movement
   *3.  Will be fired for each recorded location
   */

  BackgroundGeolocation.on('location', location => {
    console.log('on location:', location);
    location && getLocationInputAndSave(location);
  });

// In Android, there is headless mode where we can receive different events like 'heartbeat', 'location' when our app is terminated. This is not available in android.

let BackgroundGeolocationHeadlessTask = async event => {
  let location = event.params;

  switch (event.name) {
    case 'location':
      console.log('on location in killed state:', location);
      location && getLocationInputAndSave(location);
  }
};

BackgroundGeolocation.registerHeadlessTask(BackgroundGeolocationHeadlessTask);

## Expected Behavior
I should receive location updates in the killed state on a real iOS device.

## Actual Behavior
I'm not getting location updates in the killed state on a real iOS device even after moving around 3-4kms. Although I'm getting it on in the simulator using "City-run".
christocracy commented 3 years ago

Are you aware that the iOS plugin requires that the device move at least 200 meters after being terminated before your app will re-launch and tracking resume?

Set debug: true, boot your app in the iOS SImulator, simulate location with Freeway Drive then terminate your app.

Reenagrg100 commented 3 years ago

Yeah, @christocracy I'm aware of this thing and I have mentioned this also above that even after moving 3-4kms( for 1hr through the car), I'm not able to get any location. Although I'm able to get it on the simulator but not on any device. I have also made sure that permission is set to "Always allow".

christocracy commented 3 years ago

I suggest you install the SampleApp on your device and test it. Do not look at the code. Just clone the repo and install it on your device.

Reenagrg100 commented 3 years ago

Okay, I'll install it. @christocracy. Any idea if it's device model-specific or ios specific? like ios version above 14.0.

christocracy commented 3 years ago

Any idea if it's device model-specific or ios specific? like ios version above 14.0.

It's not related to device or OS. It is either your code or the environment.

Reenagrg100 commented 3 years ago

Okay, thank you so much for the quick reply @christocracy. As soon as there will be any update I'll update here.

Reenagrg100 commented 3 years ago

Are you aware that the iOS plugin requires that the device move at least 200 meters after being terminated before your app will re-launch and tracking resume?

Hey, @christocracy I'm a little confused with your response.

Are you saying that after terminating the app and moving at least 200 ms when we will re-launch the app again then we will get that one location data point and start getting the next data points further or we will be getting all the location data points during that killed state until we don't re-launch the app? Please clarify your response.

So, in short, will we get all the location data points during the killed state of the app on iOS ?

christocracy commented 3 years ago

Are you saying that after terminating the app and moving at least 200 ms when we will re-launch the app again then we will get that one location data point and start getting the next data points

When the app is terminated, you will not receive any data until your app is relaunched and tracking resumes. There will be a "hole" in your data.

Read the API docs for Config.stopOnTerminate

So, in short, will we get all the location data points during the killed state of the app on iOS ?

No. There is no data while your app remains terminated. That data is not recorded. You will have no idea about the location between the point where the device is terminated and where your app relaunches after ~200 meters.

Reenagrg100 commented 3 years ago

Read the API docs for Config.stopOnTerminate

Thanks, @christocracy! I got it right, also read the docs again just to be sure.

As per the docs, it's been said. Terminate the app -> move at least 200ms => Behind the scene our app will get the reboot in the background and tracking resumes. So, post that we will get all the data points in the app's terminated state. Right??

And after reading your above response, somewhere it's reflecting that we are not gonna get data during the whole terminated state of the app until we don't re-launch the app again. ( means the state of the app doesn't change from terminated to foreground).

Please Note:- I'm not expecting data for that at least 200ms distance. I'm only expecting only post that but in the terminated state itself.

Reenagrg100 commented 3 years ago

I suggest you install the SampleApp on your device and test it. Do not look at the code. Just clone the repo and install it on your device.

Also, is this app available on the AppStore? I can see it on the PlayStore for android but not on AppStore for ios.

christocracy commented 3 years ago

Also, is this app available on the AppStore?

No. See the Github repo for instructions on how to manually build the app onto your iOS device.

Reenagrg100 commented 3 years ago

Read the API docs for Config.stopOnTerminate

Thanks, @christocracy! I got it right, also read the docs again just to be sure.

As per the docs, it's been said.

Terminate the app -> move at least 200ms => Behind the scene our app will get the reboot in the background and tracking resumes. So, post that we will get all the data points in the app's terminated state. Right??

And after reading your above response, somewhere it's reflecting that we are not gonna get data during the whole terminated state of the app until we don't re-launch the app again. ( means the state of the app doesn't change from terminated to foreground).

Please Note:- I'm not expecting data for that at least 200ms distance. I'm only expecting only post that but in the terminated state itself.

Please clarify this also @christocracy. Thanks in advance for your help.

christocracy commented 3 years ago
Reenagrg100 commented 3 years ago
  • When an iOS app is terminated you get no data.
  • When the device moves about 200 meters after being terminated, the OS will automatically relaunch your app in the background (just as if launched by the user from the home-screen) and tracking resumes. Your app is no longer terminated -- it is now running in the background.

So, here you mean the user doesn't need to manually re-launch it. OS itself will take care of re-booting the device behind the scene and also user won't be seeing it in the background apps on his/her device. So, for the user app is still terminated and for OS, it's in the background. Am I getting it right? @christocracy.

christocracy commented 3 years ago

So, here you mean the user doesn't need to manually re-launch it

Correct. This is easily demonstrated in the iOS simulator.

Reenagrg100 commented 3 years ago

Hey, @christocracy I know you have closed this issue but this is not closed for me.

I was reading this same issue reported by someone https://github.com/transistorsoft/react-native-background-geolocation/issues/1166 and you mentioned here that wifi must be enabled from the device settings. So, here do you mean by the "internet should be available on the device"? It could be Mobile internet also, right? or Is it particularly only "wifi".

Just curious to know if you mean by only "wifi", then generally we don't have any access to wifi while moving, we use our mobile internet in that case.

christocracy commented 3 years ago

iOS needs Wifi enabled so it can use the geolocation of detected wifi routers to evaluate geofences. It doesn't mean the device needs to actually be authenticated with a router.

Both iOS and Android maintain a massive database of Wifi routers and their corresponding geolocation throughout the world.

Wifi needs to be ENABLED, not DISABLED.