@@ -32,22 +32,32 @@ 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+ /// - earliestBeginInSeconds: Optional delay before scheduling next occurrence
44+ /// - inputData: Input data passed from the Dart side (may be nil)
3545 @available ( iOS 13 . 0 , * )
36- public static func handlePeriodicTask( identifier: String , task: BGAppRefreshTask , earliestBeginInSeconds: Double ? ) {
46+ public static func handlePeriodicTask( identifier: String , task: BGAppRefreshTask , earliestBeginInSeconds: Double ? , inputData : [ String : Any ] ? ) {
3747 guard let callbackHandle = UserDefaultsHelper . getStoredCallbackHandle ( ) ,
3848 let _ = FlutterCallbackCache . lookupCallbackInformation ( callbackHandle)
3949 else {
4050 logError ( " [ \( String ( describing: self ) ) ] \( WMPError . workmanagerNotInitialized. message) " )
4151 return
4252 }
4353
44- // If frequency is not provided it will default to 15 minutes
54+ // Schedule the next occurrence (iOS will determine actual timing based on usage patterns)
4555 schedulePeriodicTask ( taskIdentifier: task. identifier, earliestBeginInSeconds: earliestBeginInSeconds ?? ( 15 * 60 ) )
4656
4757 let operationQueue = OperationQueue ( )
4858 let operation = createBackgroundOperation (
4959 identifier: task. identifier,
50- inputData: nil ,
60+ inputData: inputData ,
5161 backgroundMode: . backgroundPeriodicTask( identifier: identifier)
5262 )
5363
@@ -57,6 +67,13 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
5767 operationQueue. addOperation ( operation)
5868 }
5969
70+ /// Starts a one-off background task with the specified input data.
71+ ///
72+ /// - Parameters:
73+ /// - identifier: Task identifier
74+ /// - taskIdentifier: iOS background task identifier for lifecycle management
75+ /// - inputData: Input data to pass to the Flutter task
76+ /// - delaySeconds: Delay before task execution
6077 @available ( iOS 13 . 0 , * )
6178 public static func startOneOffTask( identifier: String , taskIdentifier: UIBackgroundTaskIdentifier , inputData: [ String : Any ] ? , delaySeconds: Int64 ) {
6279 let operationQueue = OperationQueue ( )
@@ -70,20 +87,29 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
7087 operationQueue. addOperation ( operation)
7188 }
7289
90+ /// Registers a periodic background task with iOS BGTaskScheduler.
91+ ///
92+ /// This method must be called during app initialization (typically in AppDelegate)
93+ /// to register the task identifier with iOS. The actual task scheduling with inputData
94+ /// happens later when called from the Dart/Flutter side.
95+ ///
96+ /// - Parameters:
97+ /// - identifier: Unique task identifier that matches the one used in Dart
98+ /// - frequency: Optional frequency hint in seconds (iOS may ignore this based on usage patterns)
99+ ///
100+ /// - Note: This registers the task handler only. Use Workmanager.registerPeriodicTask()
101+ /// from Dart to actually schedule the task with inputData.
73102 @objc
74103 public static func registerPeriodicTask( withIdentifier identifier: String , frequency: NSNumber ? ) {
75104 if #available( iOS 13 . 0 , * ) {
76- var frequencyInSeconds : Double ?
77- if let frequencyValue = frequency {
78- frequencyInSeconds = frequencyValue. doubleValue
79- }
80-
81105 BGTaskScheduler . shared. register (
82106 forTaskWithIdentifier: identifier,
83107 using: nil
84108 ) { task in
85109 if let task = task as? BGAppRefreshTask {
86- handlePeriodicTask ( identifier: identifier, task: task, earliestBeginInSeconds: frequencyInSeconds)
110+ // Retrieve the stored inputData for this periodic task
111+ let storedInputData = UserDefaultsHelper . getStoredPeriodicTaskInputData ( forTaskIdentifier: task. identifier)
112+ handlePeriodicTask ( identifier: identifier, task: task, earliestBeginInSeconds: nil , inputData: storedInputData)
87113 }
88114 }
89115 }
@@ -102,6 +128,12 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
102128 }
103129 }
104130
131+ /// Registers a background processing task with iOS BGTaskScheduler.
132+ ///
133+ /// This method must be called during app initialization (typically in AppDelegate)
134+ /// to register the task identifier with iOS for background processing tasks.
135+ ///
136+ /// - Parameter identifier: Unique task identifier that matches the one used in Dart
105137 @objc
106138 public static func registerBGProcessingTask( withIdentifier identifier: String ) {
107139 if #available( iOS 13 . 0 , * ) {
@@ -140,6 +172,12 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
140172
141173 // MARK: - FlutterPlugin conformance
142174
175+ /// Sets the plugin registrant callback for background task execution.
176+ ///
177+ /// This callback is used to register additional plugins when background tasks
178+ /// run in a separate Flutter engine instance.
179+ ///
180+ /// - Parameter callback: The callback to register plugins in the background engine
143181 @objc
144182 public static func setPluginRegistrantCallback( _ callback: @escaping FlutterPluginRegistrantCallback ) {
145183 flutterPluginRegistrantCallback = callback
@@ -191,6 +229,13 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
191229
192230 executeIfSupportedVoid ( completion: completion, feature: " PeriodicTask " ) {
193231 let initialDelaySeconds = Double ( request. initialDelaySeconds ?? 0 )
232+
233+ // Store the inputData for later retrieval when the task executes
234+ UserDefaultsHelper . storePeriodicTaskInputData (
235+ request. inputData as? [ String : Any ] ,
236+ forTaskIdentifier: request. uniqueName
237+ )
238+
194239 WorkmanagerPlugin . schedulePeriodicTask (
195240 taskIdentifier: request. uniqueName,
196241 earliestBeginInSeconds: initialDelaySeconds
0 commit comments