@@ -32,22 +32,32 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
32
32
operationQueue. addOperation ( operation)
33
33
}
34
34
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)
35
45
@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 ] ? ) {
37
47
guard let callbackHandle = UserDefaultsHelper . getStoredCallbackHandle ( ) ,
38
48
let _ = FlutterCallbackCache . lookupCallbackInformation ( callbackHandle)
39
49
else {
40
50
logError ( " [ \( String ( describing: self ) ) ] \( WMPError . workmanagerNotInitialized. message) " )
41
51
return
42
52
}
43
53
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)
45
55
schedulePeriodicTask ( taskIdentifier: task. identifier, earliestBeginInSeconds: earliestBeginInSeconds ?? ( 15 * 60 ) )
46
56
47
57
let operationQueue = OperationQueue ( )
48
58
let operation = createBackgroundOperation (
49
59
identifier: task. identifier,
50
- inputData: nil ,
60
+ inputData: inputData ,
51
61
backgroundMode: . backgroundPeriodicTask( identifier: identifier)
52
62
)
53
63
@@ -57,6 +67,13 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
57
67
operationQueue. addOperation ( operation)
58
68
}
59
69
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
60
77
@available ( iOS 13 . 0 , * )
61
78
public static func startOneOffTask( identifier: String , taskIdentifier: UIBackgroundTaskIdentifier , inputData: [ String : Any ] ? , delaySeconds: Int64 ) {
62
79
let operationQueue = OperationQueue ( )
@@ -70,20 +87,29 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
70
87
operationQueue. addOperation ( operation)
71
88
}
72
89
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.
73
102
@objc
74
103
public static func registerPeriodicTask( withIdentifier identifier: String , frequency: NSNumber ? ) {
75
104
if #available( iOS 13 . 0 , * ) {
76
- var frequencyInSeconds : Double ?
77
- if let frequencyValue = frequency {
78
- frequencyInSeconds = frequencyValue. doubleValue
79
- }
80
-
81
105
BGTaskScheduler . shared. register (
82
106
forTaskWithIdentifier: identifier,
83
107
using: nil
84
108
) { task in
85
109
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)
87
113
}
88
114
}
89
115
}
@@ -102,6 +128,12 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
102
128
}
103
129
}
104
130
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
105
137
@objc
106
138
public static func registerBGProcessingTask( withIdentifier identifier: String ) {
107
139
if #available( iOS 13 . 0 , * ) {
@@ -140,6 +172,12 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
140
172
141
173
// MARK: - FlutterPlugin conformance
142
174
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
143
181
@objc
144
182
public static func setPluginRegistrantCallback( _ callback: @escaping FlutterPluginRegistrantCallback ) {
145
183
flutterPluginRegistrantCallback = callback
@@ -191,6 +229,13 @@ public class WorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate, FlutterPlugin
191
229
192
230
executeIfSupportedVoid ( completion: completion, feature: " PeriodicTask " ) {
193
231
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
+
194
239
WorkmanagerPlugin . schedulePeriodicTask (
195
240
taskIdentifier: request. uniqueName,
196
241
earliestBeginInSeconds: initialDelaySeconds
0 commit comments