LocationUpdates/Geofencing not running in background

Correction, sorry - I forgot I turned off “Always” tracking in the expo client & after turning back on, I am receiving it from the client, but not the standalone. I determine this by prefixing the log with ‘dev/’ if it’s in dev, so it is working in the client but not the standalone.

Here’s what I have for startLocationUpdatesAsync that’s working for me as a standalone in iOS.

  // Define Task in the task manager
  TaskManager.defineTask('GEO_LOCATION_UPDATE', ({ data: { locations }, error }) => {
    if (error) {
      Amplitude.logEventWithProperties("GEO_LOCATION_UPDATE_ERROR", { error, locations });
      return;
    }
    Amplitude.logEventWithProperties("GEO_LOCATION_UPDATE", { locations });
  });
  //Register task 
Location.startLocationUpdatesAsync('GEO_LOCATION_UPDATE', {
        accuracy: 4,
        timeInterval: (1000 * 60 * 2),
        distanceInterval: 100,
      }).then(result => console.log('GEO LOCATION UPDATE', result))
1 Like

Hey guys, sorry I didn’t have time to take a look on that, but I’ll try to find some time soon :wink:
So, to sum up, background location works for you on both Expo Client and standalone app, but geofencing works only in Expo Client, right?

No worries! Correct, except I have not gotten geofencing to work in the background in either the client or the standalone

Thanks @tsapeta for taking a look.
Background location (Location.startLocationUpdatesAsync) works for me on both Expo Client and standalone app.
Geofencing (Location.startGeofencingAsync) doesn’t work at all on either.

I just ran another test with both tasks registered and Location Update is working fine, I entered and exited the location specified in my geofencing and didn’t receive a single geofencing event; though location update worked perfectly and reported correct lat/longs all along the way.

It seems like I can build my own geofencing algo using location updates, but it would be nice to use something standard.

1 Like

Here’s another thread that I started earlier with more details on trying to get geofencing working.

To add to the questions above, is it possible to do this on iOS without permanently showing the blue flashing bar at the top of the screen?

Also, does location reporting stop if the user closes the app?

Location does stop reporting (as configured) and switches to background location reporting, which is either after a “significant” location change (500m according to apple’s docs) or if the device enters/exits a geofence

Hi,
Thank you for giving us the example, I got this error “GEO LOCATION UPDATE undefined” when testing your example.
Could you help me with this?
Thanks

Hmmm, is your TaskManager.defineTask(‘GEO_LOCATION_UPDATE’) running before Location.startLocationUpdateAsync.

From Expo documentation
TaskManager.defineTask(taskName, task) Defines task function. It must be called in the global scope of your JavaScript bundle. In particular, it cannot be called in any of React lifecycle methods like componentDidMount. This limitation is due to the fact that when the application is launched in the background, we need to spin up your JavaScript app, run your task and then shut down — no views are mounted in this scenario.

Ideally all your TaskManager.defineTasks run outside of components, somewhere in the initial app setup code.
Then you fire off startLocationUpdateAsync in one of your components based on user input or something else.

Thanks, I figure out what’s wrong! I am wondering how to test the background location on the simulator when you exit the app?

In your simulator, click on debug > location > Freeway Drive and it’ll simulate you moving around. image

Do you know will this work when you quit the app?
Thanks!

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:

2 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.