-
Notifications
You must be signed in to change notification settings - Fork 272
Description
Required Reading
- Confirmed
Plugin Version
4.18.0
Flutter Doctor
[✓] Flutter (Channel stable, 3.35.7, on macOS 26.1 25B78 darwin-arm64, locale en-IL)
[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 26.1.1)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2025.2)
[✓] VS Code (version 1.100.2)
[✓] Connected device (4 available)
[✓] Network resources
• No issues found!Mobile operating-system(s)
- iOS
- Android
Device Manufacturer(s) and Model(s)
iPhone 13 Pro
Device operating-systems(s)
iOS 26.2
What happened?
Location tracking is sometimes not enabled after calling BackgroundGeolocation.start.
Actual Behavior:
Launched app, called BackgroundGeolocation.ready which returned State.enabled == false, and then called BackgroundGeolocation.start which returned State.enabled == true. Great.
Restarted the app, called BackgroundGeolocation.ready which returned State.enabled == false, and then called BackgroundGeolocation.start which returned State.enabled == false. Not so great.
If I set stopOnTerminate: false in the config, then on restart, BackgroundGeolocation.ready returned State.enabled == true and thus location tracking is active even on restart.
Expected Behavior:
Launch app, call BackgroundGeolocation.ready which returns State.enabled == false, then call BackgroundGeolocation.start which returns State.enabled == true.
Restart the app, call BackgroundGeolocation.ready which returns State.enabled == false, then call BackgroundGeolocation.start which returns State.enabled == true.
It seems weird/wrong to set stopOnTerminate: false for location tracking to be always be enabled on launch.
Plugin Code and/or Config
// --------------------------------
// Configure the plugin
// --------------------------------
bg.State state = await bg.BackgroundGeolocation.ready(
_getBGConfig(locAuthAlways: forceAlwaysLocPerm),
);
debugPrint('PRE-START state $state');
if (!state.enabled) {
debugPrint('[LOC UTILS] >>> starting bg <<<');
// --------------------------------
// Start the plugin
// --------------------------------
state = await bg.BackgroundGeolocation.start();
debugPrint('POST-START state $state');
}
debugPrint('[LOC UTILS] >>> INITIALIZED <<<');
...
bg.Config _getBGConfig({bool locAuthAlways = false}) {
return bg.Config(
desiredAccuracy: bg.Config.DESIRED_ACCURACY_HIGH,
distanceFilter: 10.0,
autoSync: false,
maxRecordsToPersist: 1000,
persistMode: bg.Config.PERSIST_MODE_NONE,
logMaxDays: 0,
locationAuthorizationRequest: locAuthAlways ? 'Always' : 'WhenInUse',
foregroundService: true,
backgroundPermissionRationale: bg.PermissionRationale(
title: t('Allow {applicationName} to access to this device\'s location in the background?'),
message: t(
'In order to track your activity in the background, please enable {backgroundPermissionOptionLabel} location permission',
),
positiveAction: t("Change to {backgroundPermissionOptionLabel}"),
negativeAction: t("Cancel"),
),
locationAuthorizationAlert: {
'titleWhenNotEnabled': t('Location Services not enabled'),
'titleWhenOff': t('Location Services are OFF'),
'instructions': tf(t("You must enable <> in location services"), 'Always'),
'cancelButton': t('Cancel'),
'settingsButton': t('Settings'),
},
logLevel: kDebugMode ? bg.Config.LOG_LEVEL_OFF : bg.Config.LOG_LEVEL_WARNING,
reset: true,
// NOTE: ios seems to have weird behavior where the location-tracking is "enabled" at alternating app starts...
// meaning if it works at one app-start, then the next it will not, and so on...
// setting this "stopOnTerminate: false" seems to resolve that.
// stopOnTerminate: Platform.isIOS ? false : null,
heartbeatInterval: kHeartbeatIntervalSeconds,
);
}Relevant log output
stopOnTerminate: null
INITIAL LAUNCH
flutter: PRE-START state [State enabled: false, isMoving: false, trackingMode: 1, desiredAccuracy: -1, distanceFilter: 10.0, odometer: 0.0, schedulerEnabled: false, foregroundService: null]
flutter: [LOC UTILS] >>> starting bg <<<
flutter: POST-START state [State enabled: true, isMoving: false, trackingMode: 1, desiredAccuracy: -1, distanceFilter: 10.0, odometer: 0.0, schedulerEnabled: false, foregroundService: null]
flutter: [LOC UTILS] >>> INITIALIZED <<<
ON APP-RESTART
flutter: PRE-START state [State enabled: false, isMoving: false, trackingMode: 1, desiredAccuracy: -1, distanceFilter: 10.0, odometer: 0.0, schedulerEnabled: false, foregroundService: null]
flutter: [LOC UTILS] >>> starting bg <<<
flutter: POST-START state [State enabled: false, isMoving: false, trackingMode: 1, desiredAccuracy: -1, distanceFilter: 10.0, odometer: 0.0, schedulerEnabled: false, foregroundService: null]
flutter: [LOC UTILS] >>> INITIALIZED <<<
stopOnTerminate: false
INITIAL LAUNCH
flutter: PRE-START state [State enabled: false, isMoving: false, trackingMode: 1, desiredAccuracy: -1, distanceFilter: 10.0, odometer: 0.0, schedulerEnabled: false, foregroundService: null]
flutter: [LOC UTILS] >>> starting bg <<<
flutter: POST-START state [State enabled: true, isMoving: false, trackingMode: 1, desiredAccuracy: -1, distanceFilter: 10.0, odometer: 0.0, schedulerEnabled: false, foregroundService: null]
flutter: [LOC UTILS] >>> INITIALIZED <<<
ON APP-RESTART
flutter: PRE-START state [State enabled: true, isMoving: false, trackingMode: 1, desiredAccuracy: -1, distanceFilter: 10.0, odometer: 0.0, schedulerEnabled: false, foregroundService: null]
flutter: [LOC UTILS] >>> INITIALIZED <<<