Skip to content

IOS foreground task stops working after around 35 seconds #18

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
MenuItem207 opened this issue Oct 5, 2021 · 5 comments
Closed

IOS foreground task stops working after around 35 seconds #18

MenuItem207 opened this issue Oct 5, 2021 · 5 comments

Comments

@MenuItem207
Copy link

I've been using this package for my app and the implementation works fine on Android. However I've noticed that after about 35 seconds of swiping out of the app to the Home Screen, the foreground service stops updating. I'm using geolocator to get the location like so:
locationSubscription = positionStream.listen((Position position) { currentLocation = position; FlutterForegroundTask.updateService( notificationTitle: 'Current Position', notificationText: '${position.latitude}, ${position.longitude}'); })
The foreground service updates for about 35 seconds but stops after that. The WithForegroundTask widget has been wrapped around my home widget and I'm using the IOS simulator to test this. I understand the readme did mention that 'If the app is forcibly closed, the task will not work.' I'm not sure if this is a misunderstanding on my part but swiping out to the Home Screen isn't force closing the app right?

Tldr: the flutter foreground task stops updating after about 35 seconds on the iOS simulator

@Dev-hwang
Copy link
Owner

Hi, @MenuItem207

I tested it and found the same problem. It seems to be a background processing limitation on the iOS platform. I will check other ways to solve the problem.

@Dev-hwang
Copy link
Owner

Dev-hwang commented Oct 6, 2021

@MenuItem207

One good news is that you can continue to receive updates by subscribing to the stream on onStart rather than using onEvent.

Please check this code:

// The callback function should always be a top-level function.
void startCallback() {
  // The setTaskHandler function must be called to handle the task in the background.
  FlutterForegroundTask.setTaskHandler(FirstTaskHandler());
}

class FirstTaskHandler implements TaskHandler {
  StreamSubscription<Position>? streamSubscription;

  @override
  Future<void> onStart(DateTime timestamp, SendPort? sendPort) async {
    final positionStream = Geolocator.getPositionStream();
    streamSubscription = positionStream.listen((event) {
      // Update notification content.
      FlutterForegroundTask.updateService(
          notificationTitle: 'Current Position',
          notificationText: '${event.latitude}, ${event.longitude}');

      // Send data to the main isolate.
      sendPort?.send(event);
    });
  }

  @override
  Future<void> onEvent(DateTime timestamp, SendPort? sendPort) async {

  }

  @override
  Future<void> onDestroy(DateTime timestamp) async {
    await streamSubscription?.cancel();
  }
}

And add the following to your info.plist file:

<key>NSLocationWhenInUseUsageDescription</key>
<string>Used to provide geofence service.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Used to provide geofence services in the background.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Used to provide geofence services in the background.</string>
<key>UIBackgroundModes</key>
<array>
    <string>fetch</string>
    <string>location</string>
</array>

Importantly, the user must select 'Allow While Using App' for the location permission.

image

@MenuItem207
Copy link
Author

MenuItem207 commented Oct 6, 2021

Alright, the foreground task seems to be working fine now! All I did was add the fetch line to my UIBackground modes.

Although for reference, the location permission I'm using is Always and not while using App

@rohitsangwan01
Copy link

rohitsangwan01 commented Dec 16, 2021

@Dev-hwang , can you add this in Docs as well , might be usefull for Native android devlopers like me who dont have much knowledge of IOS , took lot of time to figure out why my bluetooth tasks were stoping after minimising app , even when all settings was done properly

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

@junaidlodhi22
Copy link

@Dev-hwang
Registering Pedometer.stepCountStream works in debug mode when registered in the onStart method. But it does not keep sending step count in release mode

@override
Future<void> onStart(DateTime timestamp, SendPort? sendPort) async {
  Stream stepCountStream2 = Pedometer.stepCountStream;
  stepCountStream2.listen((event) {
    print(event);
    FlutterForegroundTask.updateService(notificationTitle: 'Walking', notificationText: event.steps.toString());
  });
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants