Skip to content

🐞registerOneOffTask with initialDelay executes immediately on iOS instead of waiting for the specified delay period. #661

@denny-wongso

Description

@denny-wongso
  • I have read the README
  • I have done the setup for Android
  • I have done the setup for iOS
  • I have ran the sample app and it does not work there

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

  1. Is initialDelay supported for registerOneOffTask on iOS?
  2. Does iOS's BGTaskScheduler require minimum delay periods?
  3. Is there a workaround or correct way to schedule one-time tasks with delays on iOS?

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions