LocationUpdates/Geofencing not running in background

I have been trying to implement this feature for about a week and in my experience it does not have the ability to capture geo location data and submit it to an API after the app is quit. Also, while the app is open, the pulsing blue bar shows up at the top of the screen indicating that the app is actively tracking location (think Google Maps). I hope that I’m missing a big part of the picture on this, because in this form it’s not practical for most use cases I could think of.

From the Expo blog https://blog.expo.io/expo-sdk-v32-0-0-is-now-available-6b78f92a6c52
“If your app isn’t running when one of these events is triggered, TaskManager will boot up your app in the background and execute whichever tasks you’ve defined.” So in theory, we should be able to track the location while the app isn’t running. Maybe is not able to test using simulator and expo cli.
Please let me know if you have new findings!

Hi,

Let’s say we have three application states - foregrounded, backgrounded and terminated/suspended. The last one is also kind of a background state and is defined as an app that is completely closed (it doesn’t appear in multitasking managers). However, there are some cases where the app can be woken up from the terminated state and run some code, it could be JS code too - that’s the magic that TaskManager can do. Unfortunately waking up an app from that state is a bit limited on iOS and Android > 8.0, so simple location update cannot wake up your app, on iOS we use significant location updates feature to make it possible. When the significant location update arrives (it requires at least 500m movement!), TaskManager spins up a new JS context in a headless mode (without views), calls appropriate tasks, waits for them to be finished and destroys the context, so the app can fall asleep again, however it’s no longer in the terminated state. Afterwards, we switch back to standard location service (the app still won’t be present in the multitasking view).
Summarizing, after the significant location change that wakes up the app, you should be receiving normal location updates almost as often as in the foreground state. Also, remember that you need “Always” location permission on iOS to make it work as expected.

There is also one simple trick to trigger a significant location change. Kill your app manually when your task is started, then go to system settings, turn off location services, wait a moment and then turn it on. It may cause your app to wake up. To confirm whether the app has been woken up, I’d recommend to use Console app on macOS to see logs from your iOS device and filter them by EXTaskService :slightly_smiling_face:

I hope I helped :smile:

3 Likes

thanks for reply, so, how we can live with android devices? and will expo team do something for get locations in terminated/suspended state or maybe there is some tricks?

Wanted to update this thread because I updated a github issue with it:

I could not get background location tracking or geofencing working in the background in the simulator OR on my physical device as I move around, both as a standalone and running my published app (from my profile) in the expo app. I have all the required App.json keys, and have given both “always” permissions.

I’ve tested by

  • messing around with the location settings (for the simulator)
  • driving, entering geofences, stopping for at least 1 min with my physical device

The only time I’ve noticed the app being woken up from being entirely quit is when my real device is on the same network as the expo-cli instance running on my computer - it will wake up, connect to the expo instance, console log updates (I omitted the console log line in my code snippet), display a notification with Constants.isHeadless being true, as well as make a request to my api.

Is there a similar console we can use for Android devices?

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