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

Don't get background locations in IOS when app is killed #1155

Closed gaurav07june closed 3 years ago

gaurav07june commented 3 years ago

When the app is in foreground it works fine but when the app is killed either by user or by OS, don't get the location update.

componentDidMount(){

  BackgroundGeolocation.onLocation(this.onLocation.bind(this), this.onError.bind(this));
  BackgroundGeolocation.onMotionChange(this.onMotionChange.bind(this));
  BackgroundGeolocation.onActivityChange(this.onActivityChange.bind(this));
  BackgroundGeolocation.onProviderChange(this.onProviderChange.bind(this));
  BackgroundGeolocation.ready({
    desiredAccuracy: BackgroundGeolocation.DESIRED_ACCURACY_HIGH,
    distanceFilter: 5,
    stopTimeout: 1,
    debug: true,
    logLevel: BackgroundGeolocation.LOG_LEVEL_VERBOSE,
    stopOnTerminate: false,   // <-- Allow the background-service to continue tracking when user closes the app.
    startOnBoot: true,        // <-- Auto start tracking when device is powered-up.
    foregroundService: true,
    url : "some url here",
    autoSync: true,
    autoSyncThreshold: 10,
    isMoving: false,
    batchSync: true,
    method: "POST",
    extras: {'imei': deviceInfo.uniqueId},
    stationaryRadius: 27,
    notification: {
      sticky: true,
      title: "KOGO App Tracker",
      text: "Your live trip is started.",
      channelName: "KOGO App Tracker",
      smallIcon: "mipmap/ic_notification", // <-- defaults to app icon
      largeIcon: "mipmap/ic_launcher",
      priority: BackgroundGeolocation.NOTIFICATION_PRIORITY_HIGH
    }
  }, (state) => {
    console.log("- BackgroundGeolocation is configured and ready: ", state.enabled);

    if (!state.enabled) {
      BackgroundGeolocation.start(function() {
        console.log("- Start success");
        BackgroundGeolocation.changePace(false);
      });
    }
  });
}

}

Expected Behavior

It should fetch location in app killed state too.

Actual Behavior

When the app is killed , getting no location data

Context

christocracy commented 3 years ago

Have you read the API docs for Config.stopOnTerminate?

gaurav07june commented 3 years ago

I have set stopOnTerminate: false, which will enable tracking in background. Also, I have defined stationaryRadius: 27, which would, I think, will create a geofence of 27 meter radius. Now when I cross this 27 meter geofence, it should update the location, but it is not even I move for kilometers. Am I using some wrong config . Can u please guide me regarding this?

christocracy commented 3 years ago

Also, I have defined stationaryRadius: 27, which would, I think, will create a geofence of 27 meter radius

No. The minimum stationaryRadius that iOS will respond to is 200 meters. You can set it 0, it won't matter.

After terminating an iOS app, tracking will not resume until the device moves a minimum of 200 meters. There is no "workaround". This is a fact of life with iOS.

christocracy commented 3 years ago

I suggest you test in the iOS simulator with debug: true and simulate location with Freeway Drive.

gaurav07june commented 3 years ago

Thanks for you suggestion chris, I'll try this way. Just for a check, I was using another application for background location tracking simultaneously with my app. And there was not a single deviations in the path for that app while my app was showing long straight lines. Both were facing same device or location conditions.

christocracy commented 3 years ago

It’s impossible for any iOS app to not have its tracking disrupted by at least 200 meters after app terminate.