@@ -32,22 +32,31 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
3232 operationQueue. addOperation ( operation)
3333 }
3434
35+ /// Handles execution of a periodic background task.
36+ ///
37+ /// This method is called by iOS when a BGAppRefreshTask is triggered.
38+ /// It retrieves stored inputData and executes the Flutter task.
39+ ///
40+ /// - Parameters:
41+ /// - identifier: Task identifier
42+ /// - task: The BGAppRefreshTask instance from iOS
43+ /// - inputData: Input data passed from the Dart side (may be nil)
3544 @available ( iOS 13 . 0 , * )
36- public static func handlePeriodicTask( identifier: String , task: BGAppRefreshTask , earliestBeginInSeconds : Double ? ) {
45+ public static func handlePeriodicTask( identifier: String , task: BGAppRefreshTask , inputData : [ String : Any ] ? ) {
3746 guard let callbackHandle = UserDefaultsHelper . getStoredCallbackHandle ( ) ,
3847 let _ = FlutterCallbackCache . lookupCallbackInformation ( callbackHandle)
3948 else {
4049 logError ( " [ \( String ( describing: self ) ) ] \( WMPError . workmanagerNotInitialized. message) " )
4150 return
4251 }
4352
44- // If frequency is not provided it will default to 15 minutes
45- schedulePeriodicTask ( taskIdentifier: task. identifier, earliestBeginInSeconds: earliestBeginInSeconds ?? ( 15 * 60 ) )
53+ // Schedule the next occurrence (iOS will determine actual timing based on usage patterns)
54+ schedulePeriodicTask ( taskIdentifier: task. identifier, earliestBeginInSeconds: 15 * 60 )
4655
4756 let operationQueue = OperationQueue ( )
4857 let operation = createBackgroundOperation (
4958 identifier: task. identifier,
50- inputData: nil ,
59+ inputData: inputData ,
5160 backgroundMode: . backgroundPeriodicTask( identifier: identifier)
5261 )
5362
@@ -57,6 +66,13 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
5766 operationQueue. addOperation ( operation)
5867 }
5968
69+ /// Starts a one-off background task with the specified input data.
70+ ///
71+ /// - Parameters:
72+ /// - identifier: Task identifier
73+ /// - taskIdentifier: iOS background task identifier for lifecycle management
74+ /// - inputData: Input data to pass to the Flutter task
75+ /// - delaySeconds: Delay before task execution
6076 @available ( iOS 13 . 0 , * )
6177 public static func startOneOffTask( identifier: String , taskIdentifier: UIBackgroundTaskIdentifier , inputData: [ String : Any ] ? , delaySeconds: Int64 ) {
6278 let operationQueue = OperationQueue ( )
@@ -70,20 +86,29 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
7086 operationQueue. addOperation ( operation)
7187 }
7288
89+ /// Registers a periodic background task with iOS BGTaskScheduler.
90+ ///
91+ /// This method must be called during app initialization (typically in AppDelegate)
92+ /// to register the task identifier with iOS. The actual task scheduling with inputData
93+ /// happens later when called from the Dart/Flutter side.
94+ ///
95+ /// - Parameters:
96+ /// - identifier: Unique task identifier that matches the one used in Dart
97+ /// - frequency: Optional frequency hint in seconds (iOS may ignore this based on usage patterns)
98+ ///
99+ /// - Note: This registers the task handler only. Use Workmanager.registerPeriodicTask()
100+ /// from Dart to actually schedule the task with inputData.
73101 @objc
74102 public static func registerPeriodicTask( withIdentifier identifier: String , frequency: NSNumber ? ) {
75103 if #available( iOS 13 . 0 , * ) {
76- var frequencyInSeconds : Double ?
77- if let frequencyValue = frequency {
78- frequencyInSeconds = frequencyValue. doubleValue
79- }
80-
81104 BGTaskScheduler . shared. register (
82105 forTaskWithIdentifier: identifier,
83106 using: nil
84107 ) { task in
85108 if let task = task as? BGAppRefreshTask {
86- handlePeriodicTask ( identifier: identifier, task: task, earliestBeginInSeconds: frequencyInSeconds)
109+ // Retrieve the stored inputData for this periodic task
110+ let storedInputData = UserDefaultsHelper . getStoredPeriodicTaskInputData ( forTaskIdentifier: task. identifier)
111+ handlePeriodicTask ( identifier: identifier, task: task, inputData: storedInputData)
87112 }
88113 }
89114 }
@@ -102,6 +127,12 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
102127 }
103128 }
104129
130+ /// Registers a background processing task with iOS BGTaskScheduler.
131+ ///
132+ /// This method must be called during app initialization (typically in AppDelegate)
133+ /// to register the task identifier with iOS for background processing tasks.
134+ ///
135+ /// - Parameter identifier: Unique task identifier that matches the one used in Dart
105136 @objc
106137 public static func registerBGProcessingTask( withIdentifier identifier: String ) {
107138 if #available( iOS 13 . 0 , * ) {
@@ -140,6 +171,12 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
140171
141172 // MARK: - FlutterPlugin conformance
142173
174+ /// Sets the plugin registrant callback for background task execution.
175+ ///
176+ /// This callback is used to register additional plugins when background tasks
177+ /// run in a separate Flutter engine instance.
178+ ///
179+ /// - Parameter callback: The callback to register plugins in the background engine
143180 @objc
144181 public static func setPluginRegistrantCallback( _ callback: @escaping FlutterPluginRegistrantCallback ) {
145182 flutterPluginRegistrantCallback = callback
@@ -191,6 +228,13 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
191228
192229 executeIfSupportedVoid ( completion: completion, feature: " PeriodicTask " ) {
193230 let initialDelaySeconds = Double ( request. initialDelaySeconds ?? 0 )
231+
232+ // Store the inputData for later retrieval when the task executes
233+ UserDefaultsHelper . storePeriodicTaskInputData (
234+ request. inputData as? [ String : Any ] ,
235+ forTaskIdentifier: request. uniqueName
236+ )
237+
194238 WorkmanagerPlugin . schedulePeriodicTask (
195239 taskIdentifier: request. uniqueName,
196240 earliestBeginInSeconds: initialDelaySeconds
0 commit comments