flutter_background_service icon indicating copy to clipboard operation
flutter_background_service copied to clipboard

iOS - Release Mode - Doesn't work

Open tty47 opened this issue 2 years ago • 3 comments

Hello!

I'm having issues when using the library for iOS in Release mode, it works in the simulators, but when I test it with Testflight it doesn't work, only works in foreground, but as soon as I move the app to background it stops...

I was able to make it work adding @pragma('vm:entry-point'), this works in the simulators (in debug mode), but I cannot test it if I terminate the app on it.

my code looks like:

// I added also here, just in case
@pragma('vm:entry-point')
initializeApp() async {
  final service = FlutterBackgroundService();

  await service.configure(
    androidConfiguration: AndroidConfiguration(
      onStart: onStart,
      autoStart: true,
      isForegroundMode: true,
    ),
    iosConfiguration: IosConfiguration(
      autoStart: true,
      onForeground: onStart,
      onBackground: onIosBackground,
    ),
  );
  service.startService();
}

@pragma('vm:entry-point')
Future<bool> onIosBackground(ServiceInstance serviceInstance) async {
  return true;
}

@pragma('vm:entry-point')
onStart(ServiceInstance service) async {
  DartPluginRegistrant.ensureInitialized();
  // if (service is AndroidServiceInstance) {
  //   service.on('setAsForeground').listen((event) {
  //     service.setAsForegroundService();
  //   });

  //   service.on('setAsBackground').listen((event) {
  //     service.setAsBackgroundService();
  //   });
  // }

  service.on('stopService').listen((event) {
    service.stopSelf();
  });

// I setted it to 5" to have a quick feedback
  Timer.periodic(const Duration(seconds: 5), (timer) async {
    // if (service is AndroidServiceInstance) {
    //   service.setForegroundNotificationInfo(
    //     title: "My App Service",
    //     content: "Updated at ${DateTime.now()}",
    //   );
    // }

    print('FLUTTER BACKGROUND SERVICE: ${DateTime.now()}');
    await httpReq();

    service.invoke(
      'update',
      {
        "current_date": DateTime.now().toIso8601String(),
      },
    );
  });
}

In the Info.plist I have (among others):

	<key>UIBackgroundModes</key>
	<array>
		<string>fetch</string>
		<string>location</string>
		<string>processing</string>
		<string>remote-notification</string>
	</array>
	<key>BGTaskSchedulerPermittedIdentifiers</key>
	<array>
		<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
	</array>

I don't have the custom task identifier, so I consider is not needed to add it to the ios/Runner/AppDelegate.swift, or is it required?

SwiftFlutterBackgroundServicePlugin.taskIdentifier = "your.custom.task.identifier"

The capabilities in xcode looks like:

Screenshot 2023-09-20 at 18 05 51

My goal is to create a task in the background to update the app every 1-5h, running an HTTP request, would that be possible with this library or should I do it with the native swift?

tty47 avatar Sep 26 '23 13:09 tty47

iOS doesn’t have long running background service like android. The alternative way is using background fetch, it’s more like alarm manager in android. Please read the apple documentation about background fetch for more detail.

ekasetiawans avatar Sep 26 '23 15:09 ekasetiawans

iOS doesn’t have long running background service like android. The alternative way is using background fetch, it’s more like alarm manager in android. Please read the apple documentation about background fetch for more detail.

do you mean this library: flutter_background_fetch?

tty47 avatar Sep 26 '23 16:09 tty47