flutter_workmanager icon indicating copy to clipboard operation
flutter_workmanager copied to clipboard

PeriodicTask on iOS

Open LostInDarkMath opened this issue 4 years ago • 18 comments

Hi there, I now that there are lots of other issues where this question is discussed. I just want to ask to ask what is the best way to create a periodic task on iOS?

What I know

  • The current documentation says, that this is not supported.
  • https://github.com/fluttercommunity/flutter_workmanager/issues/295 says, that is could be supported soon
  • https://github.com/fluttercommunity/flutter_workmanager/issues/109 also raises hope
  • In https://github.com/fluttercommunity/flutter_workmanager/issues/206 is said, that small batches are no problem. But I do not found an example. I just want to make an API-Request every 15 minutes like on Android. How can I achieve this?

So I'm very confused at the moment.

  1. Does the library support periodic background tasks on iOS now? How?
  2. If not: Is this feature in development or at least on the road map?

Thank you very much in advance!

LostInDarkMath avatar Aug 23 '21 15:08 LostInDarkMath

Also in the same situation. Everything on Android works totally fine, but if I run the same code on iOS it always throws a PlatformException.

PascalGehring avatar Aug 25 '21 17:08 PascalGehring

Same situation. iOS - PlatformException(unhandledMethod("registerPeriodicTask")

karthisu avatar Sep 08 '21 05:09 karthisu

Same situation, has anyone been able to solve it ? I'm using this alternative to run in Android but not iOS.

Future<void> _fetchBackgrountLocation(
    Function callbackDispatcher, String fetchBackground) async {
  await Workmanager().initialize(
    callbackDispatcher,
    isInDebugMode: true,
  );

  if (Platform.isAndroid) {
    Workmanager().registerPeriodicTask(
      "1",
      fetchBackground,
      frequency: Duration(minutes: 15),
    );
  }
}

Stratovarius93 avatar Oct 04 '21 16:10 Stratovarius93

Same here, any update on this?

arbazKnarkz avatar Oct 05 '21 07:10 arbazKnarkz

Most of the things says [Android Only] so i think this plugin is only for Android and not for the IOS

arbazKnarkz avatar Oct 05 '21 07:10 arbazKnarkz

Part of the issue is the plugins current APIs.

registerPeriodicTask is indeed not working on iOS, because there is no equivalent.

The new BG task APIs support scheduling background work and once they are executed, you can schedule another piece of work with a minimumDelay, thus causing the illusion of periodic work. A lot of this is due to how iOS schedules background work.

PRs on documenting, testing etc are very welcome. The code is all visible in 0.5.0-dev.8.

ened avatar Jan 30 '22 22:01 ened

Thanks for your answer!

Could you please provide a minimal example of an iOS background task that schedules another which schedules another one and so on?

And I personally think that it would be a good idea to use illusional periodic task for the registerPeriodicTask function on iOS. Even if this implementation might not be perfect it is still better than no implementation at all imho.

LostInDarkMath avatar Jan 31 '22 09:01 LostInDarkMath

Part of the issue is the plugins current APIs.

registerPeriodicTask is indeed not working on iOS, because there is no equivalent.

The new BG task APIs support scheduling background work and once they are executed, you can schedule another piece of work with a minimumDelay, thus causing the illusion of periodic work. A lot of this is due to how iOS schedules background work.

For others searching for an understanding of the various iOS tasking options, intentions and limitations there is an excellent video by Apple explaining as much here . This shows how tasking is in the hands of the OS and provides a guide on the most appropriate option for your purposes.

cormalenv avatar Jan 31 '22 23:01 cormalenv

Man forget periodic, I cannot even do OneOff task on iOS. Hope they fix this soon.

saad197 avatar Feb 01 '22 07:02 saad197

Man forget periodic, I cannot even do OneOff task on iOS. Hope they fix this soon.

https://github.com/fluttercommunity/flutter_workmanager/issues/357#issuecomment-1028842173

cormalenv avatar Feb 03 '22 10:02 cormalenv

@LostInDarkMath, I have to agree that the documentation is confusing. I am also not quite sure, which things are supported on both platforms.

Maybe someone who knows the intricacies of the plugin implementation could go over the documentation (and the comments in the code) and see that it accurately reflects what the plugin is doing.

lyio avatar Feb 09 '22 07:02 lyio

it looks like flutter_local_notifications plugin has the ability to create periodic notifications in iOS: https://pub.dev/packages/flutter_local_notifications#periodically-show-a-notification-with-a-specified-interval. Also a short example of it here.

Why can't it be done the same here in the workmanager? (sorry, I'm not an iOS developer and may be missing smth)

bdshadow avatar Nov 17 '22 21:11 bdshadow

Scheduled notifications and bgAppRefreshTask are very different.

You can schedule a notification. This notification will displayed at scheduled time.

A background work is called by iOS itself and has no possibility to schedule a specific time. The call is randomized by iOS- see apple documents for bgAppRefreshTask

xunreal75 avatar Nov 18 '22 21:11 xunreal75

@Stratovarius93 did you find a solution for iOS?

Myzel394 avatar May 17 '23 15:05 Myzel394

Hello there, any news?

ziqq avatar Jul 07 '23 08:07 ziqq

[VERBOSE-2:dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(unhandledMethod("registerPeriodicTask") error, Unhandled method registerPeriodicTask, null, null)

I'm facing this issue in IOS, android is working fine.

junaidirshad098 avatar Aug 27 '23 17:08 junaidirshad098

Any updates on this?

Check https://github.com/fluttercommunity/flutter_workmanager/pull/511 which has major enhancements for iOS

absar avatar Apr 19 '24 18:04 absar