Get device location periodically, even if app is killed

Viewed 873

I can't seem to find any supporting documentation in the Expo docs or React Native docs that say whether or not this is possible, so I figured I'd ask here.

I'm creating an app that needs to periodically fetch the device's location, whether the app is in the foreground, background, but most important even if it's killed. Even a few times an hour is fine.

Do both Android and iOS even support this behaviour? And if so, can someone point me in the right direction or provide the class/API that I would need to make use of in Expo/React Native (ideally in Expo) that would allow me to fetch the device's location periodically even if the app is killed?

Thank you!

2 Answers

Generally speaking, yes, it should be possible.

On Android, this is generally possible using a Broadcast Reciever (and see the information there regarding getting user's location up from API 28).
On iOS, this is specifically possible using the Standard Location Service (specifically refer to the section "Getting Location Events in the Background").

Focusing on React Native, then again - focusing on Expo, there is a recent post discussing just that:
The best way of tracking location in background using react-native + Expo in 2020. However, the comments there indicate that it doesn't work consistently.

Therefore, since I understand you don't specifically require using Expo (you just say "ideally in Expo" :), you can try following this React Native tutorial (June 2021): React Native background location (background & terminated app) (it uses react-native-location library).

Related