Skip to content

Background using Location stop on iOS #380

Open

Description

@ygorrrteles

Hi friend, I need some help.

I'm using the foreground + fl_location package in my iOS app to keep my background process active.

It works perfectly, but only for a short period of time, around 20 minutes, the background stops triggering the onRepeat callback, only starting again after opening the app again.

I need the background service to run for a few hours with the app minimized and the screen off, just like it happens on Android.

Could you tell me if there's something wrong with my implementation?

My code snippet is basically very similar to the example project for the foreground location.

I'm using the following for testing:
Flutter 3.37,
iPhone SE 1st generation SDK 15.8.3


    FlutterForegroundTask.init(
      ....
      foregroundTaskOptions: ForegroundTaskOptions(
        eventAction: ForegroundTaskEventAction.repeat(60000),
      ),
      ....
    );


class LocationServiceHandler extends TaskHandler {
  StreamSubscription<Location>? _streamSubscription;

  @override
  Future<void> onStart(DateTime timestamp, TaskStarter starter) async {
    _streamSubscription = FlLocation.getLocationStream(Location.low).listen((location) {
///nothing
    });
  }

  @override
  void onRepeatEvent(DateTime timestamp) {
   myComplexMethod();
  }

  @override
  Future<void> onDestroy(DateTime timestamp) async {
    _streamSubscription?.cancel();
    _streamSubscription = null;
  }
}

Activity

  1. ppamorim commented on May 2, 2026

    @ppamorim

    @ygorrrteles I wonder if this could be an iOS project configuration issue rather than a flutter_foreground_task repeat-event issue.

    From what I understand, on iOS the Dart task will only continue running in the background if the app is being kept alive by one of Apple’s approved background modes. Since this example depends on FlLocation.getLocationStream(...), the iOS app needs to be properly configured for background location updates.

    Could you confirm whether the iOS target has:

    <key>UIBackgroundModes</key>
    <array>
      <string>location</string>
    </array>

    And

    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>...</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>...</string>

    I need the background service to run for a few hours with the app minimized and the screen off, just like it happens on Android.

    iOS behaves differently to Android, you need to make your application compatible with those rules and it's not responsability of flutter_foreground_task to handle that (and it's not even possible).

  2. ygorrrteles commented on May 3, 2026

    @ygorrrteles
    Author

    Yes, in my iOS project the keys are configured:

    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>...</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>...</string>
    <key>UIBackgroundModes</key>
    <array>
    
    <string>location</string>
    </array>
    

    In my test, I also enable location always.

    I'm testing with 2 iPhones.

    iPhone SE 1st generation: Background continues running for about 20 minutes after the app is minimized.

    iPhone 11: The background works as expected, remaining running for several hours.

    I did a test on the iPhone 11 with battery saver enabled, and it also had the background automatically terminated in approximately 20 minutes. Is this behavior standard on the iPhone SE, considering that this battery saver setting doesn't exist on this older phone?

  3. ppamorim commented on May 3, 2026

    @ppamorim

    For sanity check did you attempt to implement those services native and check the behavior?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions