Skip to main content
GuidesAPI ReferenceExamplesFAQ

Background Location

|

Some use cases require location access even when the app is in the background. In such cases, you'll need to configure both iOS and Android platforms appropriately. The SDK supports this scenario, but platform permissions and services must be correctly set up for it to work.

We recommend enabling background location support if your application includes features like recording, navigation, or content download (especially for maps, which can be quite large and may take a while to fetch).

Platform Specific Configurations

The following sections outline the necessary steps to enable background location tracking on both iOS and Android platforms.

iOS Setup

On iOS, you will need to update your Info.plist to request permission for background location access.

<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Location is needed for map localization and navigation.</string>

<key>UIBackgroundModes</key>
<array>
<!-- Include other modes as needed -->
<string>location</string>
<string>processing</string>
</array>

Android Setup

You'll need to declare permissions in your manifest, and then implement a foreground service to keep location updates alive.

Required AndroidManifest Changes

Make sure you include the necessary permissions and service declarations in your AndroidManifest.xml.

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<!-- Needed for foreground service -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
tip

On Android 10+ (API level 29+), you also need to request ACCESS_BACKGROUND_LOCATION explicitly at runtime. Make sure to handle this in your app's permission request flow.

warning

You should create a foreground service on Android to ensure that location updates continue when the app is in the background. Failing to do so may result in the operating system terminating your app's background processes, leading to loss of location updates.

Foreground Service Implementation

Android won’t let you run background location tracking unless you create a foreground service. Here you can find more about foreground services: Background Location Example.

You can find a sample implementation of a foreground service in the Recorder in Background example.

Sensor Configuration

To enable background location within our SDK, you’ll also need to initialize the sensor configuration accordingly in your Flutter code.

final dataSource = DataSource.createLiveDataSource();

if (dataSource == null) {
throw "Error creating data source";
}

final config = dataSource.getConfiguration(DataType.position);

config.allowsBackgroundLocationUpdates = true;

final err = dataSource.setConfiguration(type: DataType.position, config: config);

if (err != GemError.success) {
throw "Error setting data source configuration: ${err.toString()}";
}