I have an app developed by me. The app needs send the device coordinates to backend. I am use expo-location to do this.
I make the foreground getter with this code and works:
foregroundSubscription = await Location.watchPositionAsync(
{
accuracy: Location.Accuracy.BestForNavigation,
distanceInterval: FIFTY_METERS,
// distanceInterval: 1,
timeInterval: FIVE_SECONDS, // ONLY ANDROID
},
But in background, the background api expo don’t get the coordinates:
TaskManager.defineTask(
BACKGROUND_LOCATION_TASK_KEY,
({
data: { locations },
error,
}: {
data: { locations: Location.LocationObject[] };
error: any;
}) => {
const locationsBackground: Location.LocationObject[] =
locations as Location.LocationObject[];
const coordinates: Coordinate[] = [];
[... rest of code inside taskManager]
}
export function LocationContextProvider({children}: Props) {
[...rest of code]
async function backgroundWatcher() {
console.log('BACKGROUND_INIT');
const isTaskDefined = await TaskManager.isTaskDefined(
BACKGROUND_LOCATION_TASK_KEY,
);
if (!isTaskDefined) {
return;
}
const hasStarted = await Location.hasStartedLocationUpdatesAsync(
BACKGROUND_LOCATION_TASK_KEY,
);
if (hasStarted) {
await Location.stopLocationUpdatesAsync(BACKGROUND_LOCATION_TASK_KEY);
}
console.log('INIT_SERVICE_BACKGROUND');
await Location.startLocationUpdatesAsync(BACKGROUND_LOCATION_TASK_KEY, {
accuracy: Location.Accuracy.BestForNavigation,
activityType: Location.ActivityType.AutomotiveNavigation,
showsBackgroundLocationIndicator: true,
distanceInterval: FIFTY_METERS,
// distanceInterval: 1,
timeInterval: FIVE_SECONDS, // ONLY ANDROID
foregroundService: {
notificationTitle: 'TOT Agendamento',
notificationBody: 'Operação em andamento',
notificationColor: '#fff',
},
});
}
I need when the app running in the background mode, the background works. I see in a github issue the code I wrote not work on expo go app, only works in a build app. I tested in APK but don’t work too. Try I’m waiting the google analysis to send the app to play store and test in app build .aab
But I have a lot of doubts about this. Can you help me?