react-native-background-geolocation
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.
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
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”.
I tried the same, I moved ~200 meters of radius but same issue not getting location when app terminated.
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.
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);
})
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.
and while testing outside, do not disable Wifi on your device.
This issue is stale because it has been open for 30 days with no activity.
This issue was closed because it has been inactive for 14 days since being marked as stale.