react-native-background-geolocation icon indicating copy to clipboard operation
react-native-background-geolocation copied to clipboard

location not update after getting first in iOS and not working when app closed in iOS . same working fine in Android.

Open vishuk570 opened this issue 2 years ago • 7 comments

import BackgroundGeolocation from "react-native-background-geolocation";




// execute when app forground and background 
const startBackgroundLocationService = (shiftId) => {
 

    BackgroundGeolocation.ready({
        heartbeatInterval: 10,
        preventSuspend: true, // <-- Required for iOS
        enableHeadless: true,
        stopOnTerminate: false,
      }).then((state)=>{
        BackgroundGeolocation.start();
        console.log("background location",state);
      })
      
        BackgroundGeolocation.onHeartbeat(async(event) => {
        let location = await getCurrentPosition();
        console.log('[BackgroundGeolocation] - getCurrentPosition:', location);
        saveShiftData(location);
      })
};

// execute when app close/kill.
const HeadlessTask = async(event) => {
    console.log("hello",store.getState().Shift.id);
    currentShiftId = store.getState().Shift.id;
    switch (event.name) {
        case 'heartbeat':
          // Use await for async tasks
          let location = await getCurrentPosition();
          console.log('[BackgroundGeolocation HeadlessTask] - getCurrentPosition:', location);
          saveShiftData(location);
          break;
      }
};


let getCurrentPosition = () => {
    return new Promise((resolve) => {
      BackgroundGeolocation.getCurrentPosition(
      {
        timeout: 30,          // 30 second timeout to fetch location
        maximumAge: 5000,     // Accept the last-known-location if not older than 5000 ms.
        desiredAccuracy: 10,  // Try to fetch a location with an accuracy of `10` meters.
        samples: 1,
        persist: false
      },
      (location) => {
        resolve(location);
      }, (error) => {
        resolve(error);
      });
    });
  };

const stopBackgroundLocationService = () => {
    BackgroundGeolocation.stop();
};
export {
    startBackgroundLocationService,
    stopBackgroundLocationService,
    HeadlessTask
};

Expected Behavior

<I want to get updated location and also get updated location when app is close/kill in iOS >

Actual Behavior

< same location getting which is get first time and not working this when app is close/kill in iOS . all thing working good in android >

Steps to Reproduce

Context

Debug logs

Logs
PASTE_YOUR_LOGS_HERE

vishuk570 avatar Sep 27 '23 09:09 vishuk570

Did you read the api docs Config,stopOnTerminate? The behaviour of iOS vs Android in the terminated state is radically different and explained in the api docs.

also see wiki “Debugging” and “Philosophy of Operation”.

christocracy avatar Sep 27 '23 10:09 christocracy

I tried the same, I moved ~200 meters of radius but same issue not getting location when app terminated.

vishuk570 avatar Sep 27 '23 12:09 vishuk570

You can easily test this in simulator with “Freeway Drive”. Use debug: true. If it doesn’t work, you’re doing something wrong on your end.

you must ensure that .ready(config) is called each and every time your app launches. ready means “app ready /launched”. You call .ready(config) once, at each launch.

calling .ready(config) does not imply “start tracking”. Only calling .start() starts tracking. You are not obliged to call .start() as soon as .ready(config) has resolved.

christocracy avatar Sep 27 '23 12:09 christocracy

I already test it worked in simulator but when I test on physical iOS device it not worked. Also in simulator getting notification but this code doesn't execute.

  BackgroundGeolocation.onHeartbeat(async(event) => {
        let location = await getCurrentPosition();
        console.log('[BackgroundGeolocation] - getCurrentPosition:', location);
        saveShiftData(location);
      })

vishuk570 avatar Sep 27 '23 12:09 vishuk570

Go outside and move >= 1km.

You should avoid preventSuspend and onHeartbeat on iOS. For periodic locations, use react-native-background-fetch (already included as a dependency; see its README). call .getCurrentPosition() in your fetch callback.

christocracy avatar Sep 27 '23 14:09 christocracy

and while testing outside, do not disable Wifi on your device.

christocracy avatar Sep 27 '23 14:09 christocracy

This issue is stale because it has been open for 30 days with no activity.

github-actions[bot] avatar May 03 '24 01:05 github-actions[bot]

This issue was closed because it has been inactive for 14 days since being marked as stale.

github-actions[bot] avatar May 18 '24 01:05 github-actions[bot]