expo: Background Location Updates and Foreground Service Notification not working as expected on Android

Minimal reproducible example

https://snack.expo.dev/n2kWxz9zkx

Summary

I am working on a React Native application that requires tracking the user’s location in the background. To implement this feature, I am using the expo-location package. I followed the Expo documentation to set up the background location updates and the foreground service notification for Android.

While the background location updates seem to work correctly when the app is in the foreground, I have noticed that the updates stop when the app is sent to the background. This behavior contradicts the purpose of background location updates.

Additionally, even though I have set up the foreground service in the Location.startLocationUpdatesAsync call as per the documentation, the expected notification is not being generated when the app is in the foreground.

Here is the code snippet from my application where I set up the background location updates and the foreground service:

const startLocationTracking = async () => {
    await Location.startLocationUpdatesAsync(LOCATION_TASK_NAME, {
        accuracy: Location.Accuracy.Highest,
        timeInterval: 1000,
        distanceInterval: 0,
        showsBackgroundLocationIndicator: true,
        foregroundService: {
            notificationTitle: "Location Tracking",
            notificationBody: "Tracking your location for routing purposes",
            notificationColor: "#FF0000",
        },
        pausesUpdatesAutomatically: false,
        deferredUpdatesInterval: 1000,
        deferredUpdatesDistance: 0,
        deferredUpdatesTimeout: 1000,
    });
};

And here are the relevant permissions I have set in my app.json:

"android": {
  "permissions": ["ACCESS_COARSE_LOCATION", "ACCESS_FINE_LOCATION", "ACCESS_BACKGROUND_LOCATION", "FOREGROUND_SERVICE"],
  "useNextNotificationsApi": true
}

My understanding is that in android you need to start a “foreground service” in order to get “background location tracking” to work. Or rather to track location when the user navigates away from the app. I would assume that this is what the following does, but I’m not seeing this behavior.

foregroundService: {
      notificationTitle: 'Office marathon is active',
      notificationBody: 'Monitoring your location to measure total distance',
      notificationColor: '#333333',
    },

Am I missing something?

Environment

expo-env-info 1.0.5 environment info: System: OS: Windows 10 10.0.22621 Binaries: Node: 16.13.0 - C:\Program Files\nodejs\node.EXE Yarn: 1.22.18 - ~\AppData\Roaming\npm\yarn.CMD npm: 8.19.2 - C:\Program Files\nodejs\npm.CMD IDEs: Android Studio: AI-212.5712.43.2112.8815526 npmPackages: expo: ~48.0.15 => 48.0.16 react: 18.2.0 => 18.2.0 react-native: 0.71.7 => 0.71.7 Expo Workflow: managed

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Reactions: 4
  • Comments: 21

Most upvoted comments

So I found that this doesn’t work when you are using background location tracking in development mode using Expo Go. It works if you build an APK and install it on your device! Phew!