Version
| Technology |
Version |
| Workmanager version |
0.9.0+3 |
| Xcode version |
26.1.1 |
| Swift version |
|
| iOS deployment target |
14 |
Describe the error
Describe error
When calling registerOneOffTask with initialDelay: Duration(minutes: 15), the background task should execute approximately 15 minutes later but it is executing immediately.
Output of flutter doctor -v
Channel stable, 3.38.7
Dart Code (minimum code snippet)
import 'dart:io';
import 'package:workmanager/workmanager.dart';
import 'package:shared_preferences/shared_preferences.dart';
@pragma('vm:entry-point')
void callbackDispatcher() {
Workmanager().executeTask((task, inputData) async {
final executionTime = DateTime.now();
print('🔔 TASK EXECUTED AT: $executionTime');
print('🔔 Task name: $task');
final scheduledForMs = inputData?['scheduled_time'] as int?;
if (scheduledForMs != null) {
final scheduledFor = DateTime.fromMillisecondsSinceEpoch(scheduledForMs);
final difference = executionTime.difference(scheduledFor);
print('⏰ Scheduled for: $scheduledFor');
print('⏰ Difference: ${difference.inMinutes} minutes (${difference.inSeconds} seconds)');
}
return Future.value(true);
});
}
class BackgroundLocationService {
Future start() async {
final now = DateTime.now();
final delay = const Duration(minutes: 15);
final scheduledTime = now.add(delay);
final taskId = "com.example.myApp.alertTask";
await Workmanager().cancelByUniqueName(taskId);
await Workmanager().registerOneOffTask(
taskId,
taskId,
initialDelay: delay,
inputData: {
'scheduled_time': scheduledTime.millisecondsSinceEpoch,
},
existingWorkPolicy: ExistingWorkPolicy.replace,
constraints: Constraints(
networkType: NetworkType.connected,
),
);
}
}
iOS Configuration
Info.plist:
UIBackgroundModes
fetch
processing
location
BGTaskSchedulerPermittedIdentifiers
com.example.myApp.alertTask
AppDelegate.swift:
import UIKit
import Flutter
import workmanager
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
WorkmanagerPlugin.setPluginRegistrantCallback { registry in
GeneratedPluginRegistrant.register(with: registry)
}
WorkmanagerPlugin.registerTask(withIdentifier: "com.example.myApp.alertTask")
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
Questions
- Is
initialDelay supported for registerOneOffTask on iOS?
- Does iOS's
BGTaskScheduler require minimum delay periods?
- Is there a workaround or correct way to schedule one-time tasks with delays on iOS?
Version
Describe the error
Describe error
When calling
registerOneOffTaskwithinitialDelay: Duration(minutes: 15), the background task should execute approximately 15 minutes later but it is executing immediately.Output of
flutter doctor -vChannel stable, 3.38.7
Dart Code (minimum code snippet)
iOS Configuration
Info.plist:
UIBackgroundModes fetch processing location BGTaskSchedulerPermittedIdentifiers com.example.myApp.alertTaskAppDelegate.swift:
Questions
initialDelaysupported forregisterOneOffTaskon iOS?BGTaskSchedulerrequire minimum delay periods?