flutter_workmanager icon indicating copy to clipboard operation
flutter_workmanager copied to clipboard

🐞Bug - initDelay in registerPeriodicTask is now useless

Open BigYajuu opened this issue 9 months ago • 3 comments

  • [x] I have read the README
  • [x] I have done the setup for Android
  • [x] I have done the setup for iOS
  • [x] I have ran the sample app and it does not work there
Technology Version
Workmanager version main branch
Xcode version -
Swift version -
iOS deployment target -

Describe the error Note: I'm using this plugin's main branch.

When I invoke 'registerPeriodicTask' with initDelay and frequency durations stated I expect the callback to get called after initDelay duration from now, then gets called again in frequency.

Problem: initDelay was completely ignored and the first periodic task runs after stated frequency duration. It's weird because before I swapped to this branch, it works just fine.

Bumping back to the official pubspec version won't solve my issue because there's #588 .

Sample Code

  static Future testRunPeriodic() async {
    await Workmanager().registerPeriodicTask(
      "peridicChicken",
      "periodicChickenName",
      initialDelay: const Duration(minutes: 2),
      frequency: const Duration(minutes: 15),
      inputData: {
        "isTest": true,
      },
    );
  }

We can see that initDelay is set to 2 minutes, but the callback gets invoked in 15 minutes instead.

BigYajuu avatar Mar 21 '25 04:03 BigYajuu

@BigYajuu I saw the same and I've been doing some tests. I think this is not this package's fault, but instead a consequence of upgrading the native WorkManager version as per this line here that bumps it v2.9.0:

https://github.com/fluttercommunity/flutter_workmanager/blob/4ce065135dc1b91fee918f81596b42a56850391d/workmanager/android/build.gradle#L45

I tried downgrading to WorkManager 2.7.1, which is the version that flutter_workmanagerv0.5.2 was using, and I think it has reverted the issue.

android/app/build.gradle

configurations.all {
    resolutionStrategy.eachDependency { details ->
        if (details.requested.group == 'androidx.work' && 
            (details.requested.name == 'work-runtime' || details.requested.name == 'work-runtime-ktx')) {
            details.useVersion '2.7.1'
        }
    }
}

I haven't tried upgrading (to 2.10.0), instead of downgrading, but it would be interesting to try. I'm not sure if this is something that is broken as well in the native WorkManager... or, instead (which would be bad news) something they have decided to patch so that there's no way around the 15 minutes limitation.

Manuito83 avatar Apr 02 '25 16:04 Manuito83

Did you manage to fix it? I’m facing the same issue — it either gets called at random or not at all.

flutter v3.32 workmanager v0.9.0+2 device pixel 6a (Android 16)

`@pragma('vm:entry-point') void callbackDispatcher() { Workmanager().executeTask((task, inputData) async { if (task == "widget_update") { print("Updating widget background"); await HomeWidgetService.refreshWidgetBackground(); } return Future.value(true); }); }

@pragma('vm:entry-point') class WorkmanagerService { static Future initialize() async { if (!Platform.isAndroid) return; var widgets = await HomeWidget.getInstalledWidgets(); if (widgets.isEmpty) return; Workmanager().initialize(callbackDispatcher); await _scheduleMidnightTask(); }

static Future _scheduleMidnightTask() async { final currentTime = DateTime.now(); final targetTime = DateTime( currentTime.year, currentTime.month, currentTime.day + 1, 0, 1); final initialDelay = targetTime.difference(currentTime); await Workmanager().registerPeriodicTask( 'widget_update_task', // Unique task ID "widget_update", // The task to execute frequency: const Duration(days: 1), // Repeat every 24 hours initialDelay: initialDelay, // Delay until the first execution existingWorkPolicy: ExistingPeriodicWorkPolicy .update, // Replace any existing task with the same ID ); } }`

vinz-mehra avatar Aug 13 '25 20:08 vinz-mehra

Did you manage to fix it? I’m facing the same issue — it either gets called at random or not at all.

flutter v3.32 workmanager v0.9.0+2 device pixel 6a (Android 16)

`@pragma('vm:entry-point') void callbackDispatcher() { Workmanager().executeTask((task, inputData) async { if (task == "widget_update") { print("Updating widget background"); await HomeWidgetService.refreshWidgetBackground(); } return Future.value(true); }); }

@pragma('vm:entry-point') class WorkmanagerService { static Future initialize() async { if (!Platform.isAndroid) return; var widgets = await HomeWidget.getInstalledWidgets(); if (widgets.isEmpty) return; Workmanager().initialize(callbackDispatcher); await _scheduleMidnightTask(); }

static Future _scheduleMidnightTask() async { final currentTime = DateTime.now(); final targetTime = DateTime( currentTime.year, currentTime.month, currentTime.day + 1, 0, 1); final initialDelay = targetTime.difference(currentTime); await Workmanager().registerPeriodicTask( 'widget_update_task', // Unique task ID "widget_update", // The task to execute frequency: const Duration(days: 1), // Repeat every 24 hours initialDelay: initialDelay, // Delay until the first execution existingWorkPolicy: ExistingPeriodicWorkPolicy .update, // Replace any existing task with the same ID ); } }`

I managed to fix my issue through solution proposed by this user: https://github.com/fluttercommunity/flutter_workmanager/issues/588#issuecomment-2857758988

It seems like you need refer to a package version that is compatible to Flutter 3.29 inside pubspec.

I'm currently running on Flutter 3.32.6 (Dart 3.8.1) and this solution is still compatible.

BigYajuu avatar Aug 15 '25 02:08 BigYajuu