I’m trying to set up background location tracking with the new SDK 32 release. I read that TaskManager.defineTask() needs to be called in the global scope of my app. I have it being called in App.js
import { TaskManager } from 'expo';
import { BACKGROUND_LOCATION_UPDATES_TASK} from './tasks';
TaskManager.defineTask(BACKGROUND_LOCATION_UPDATES_TASK, ({data, error}) => {
if(error){
return;
}
if(data){
try{
const { locations } = data;
console.log('locations: ', + locations);
} catch(error){
console.log(error);
}
}
I have the initiation of Location.startLocationUpdatesAsync() in my settings screen. When the user toggles the switch to on, the location update should start. Here is an example of the logic.
toggleLocationTracking = async() => {
const { trackingOn } = this.state;
const { status } = await Permissions.askAsync(Permissions.LOCATION);
let isRegistered = await
TaskManager.isTaskRegisteredAsync(BACKGROUND_LOCATION_UPDATES_TASK);
if(!trackingOn && isRegistered && status=='granted'){
await Location.startLocationUpdatesAsync(BACKGROUND_LOCATION_UPDATES_TASK, {
accuracy: Location.Accuracy.High,
timeInterval: 2500,
distanceInterval: 5,
showsBackgroundLocationIndicator: false
});
}
... more code
}
Additionally, when the user toggles the switch off, i run Location.stopLocationUpdatesAsync(BACKGROUND_LOCATION_UPDATES_TASK);
When I try to toggle on my switch, the task is still not registered.