How to do location tracking in background to prevent app from being killed

From the documentation, Expo briefly introduces BackgroundFetch and Location as the only two options to handle background task. My scenario is triggering an API call every 10 seconds if there is a location change when the app is in the background.

I have followed the Location API " startLocationUpdatesAsync" from: https://docs.expo.io/versions/v32.0.0/sdk/location/

I have also defined and been able to run:

import { TaskManager } from 'expo';

TaskManager.defineTask(YOUR_TASK_NAME, ({ data: { locations }, error }) => {
  if (error) {
    // check `error.message` for more details.
    return;
  }
  console.log('Received new locations', locations);
});

And I am able to see the log in the terminal if the location changes. The location is also logged into the console when the app is in the background, however it only happens for a few seconds before the app just turns silently idle afterwards, even when the real location changes which should trigger the function call in the Task Manger.

At this time, only when I open the app to the foreground will it continuing to log new location. Otherwise, it will be as good as being killed.

Is there a way to keep background task running stably? Especially on Android 8 where BackgroundFetch from iOS is not supported and background tasks are generally restricted by the OS?

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