Implementing TaskManager with react-navigation 5

Hey all!

  1. SDK Version: 37
  2. Platforms(Android/iOS/web/all): all

I am using react navigatoin 5, so the main App is now a function and not a class. Due to this reason I am facing an issue of implementing expo’s TaskManager and BackgroundLocation.

Here is some of the code:

const LOCATION_TASK_NAME = 'background-location-task';

TaskManager.defineTask(LOCATION_TASK_NAME, ({ data, error }) => {
  if (error) {
    console.log(error);
    return;
  }
  if (data) {
    const { locations } = data;
    // do something with the locations captured in the background
    console.log('this is finally working');
  }
});

async function locFunction() {
  const { status } = await Location.requestPermissionsAsync();
  if (status === 'granted') {
    console.log('Background location is on');
    await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
        accuracy: Location.Accuracy.Highest,
        timeInterval: 5000
  });
  }
}

export default function App() {

 locFunction();

  return (
    <NavigationContainer>
            <StackNavigator />
    </NavigationContainer>
  );

}

It throws the error: “TaskManager.defineTask must be called during initialization phase!”, but I don’t know where to does the initilization phase is in this situation… Should I call the function from another class? Also, I can’t make App() async, tried it already…

In general, I want the app to check the location of the user every once in a while when the app is in the background, and then use geoQuery (of geolocation) and maybe send notifications…

Thanks for your help!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.