Skip to content

Commit 70291cd

Browse files
committed
- [ios] allows to register custom task id
1 parent 8469328 commit 70291cd

File tree

9 files changed

+125
-23
lines changed

9 files changed

+125
-23
lines changed

README.md

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void main() {
3535
callbackDispatcher, // The top level function, aka callbackDispatcher
3636
isInDebugMode: true // If enabled it will post a notification whenever the task is running. Handy for debugging tasks
3737
);
38-
Workmanager().registerOneOffTask("1", "simpleTask"); //Android only (see below)
38+
Workmanager().registerOneOffTask("task-identifier", "simpleTask");
3939
runApp(MyApp());
4040
}
4141
```
@@ -90,7 +90,7 @@ iOS supports **One off tasks** with a few basic constraints:
9090

9191
```dart
9292
Workmanager().registerOneOffTask(
93-
"1", // Ignored on iOS
93+
"task-identifier",
9494
simpleTaskKey, // Ignored on iOS
9595
initialDelay: Duration(minutes: 30),
9696
constraints: Constraints(
@@ -103,7 +103,29 @@ Workmanager().registerOneOffTask(
103103
);
104104
```
105105

106-
Tasks registered this way will appear in the callback dispatcher using as `Workmanager.iOSBackgroundProcessingTask`.
106+
**NOTE**
107+
108+
- We have to register in ApplicationDelegate class as below example
109+
110+
```swift
111+
WorkmanagerPlugin.registerTask(withIdentifier: "task-identifier")
112+
```
113+
114+
- We must also define your `task-identifier` in `info.plist` file
115+
116+
```
117+
<dict>
118+
....
119+
<key>BGTaskSchedulerPermittedIdentifiers</key>
120+
<array>
121+
<string>workmanager.background.task</string>
122+
<string>task-identifier</string>
123+
</array>
124+
....
125+
</dict>
126+
```
127+
128+
- The task identifier should be prefixed with app bundle id
107129

108130
For more information see the [BGTaskScheduler documentation](https://developer.apple.com/documentation/backgroundtasks).
109131

@@ -119,14 +141,14 @@ Two kinds of background tasks can be registered :
119141
```dart
120142
// One off task registration
121143
Workmanager().registerOneOffTask(
122-
"1",
144+
"oneoff-task-identifier",
123145
"simpleTask"
124146
);
125147
126148
// Periodic task registration
127149
Workmanager().registerPeriodicTask(
128-
"2",
129-
"simplePeriodicTask",
150+
"periodic-task-identifier",
151+
"simplePeriodicTask",
130152
// When no frequency is provided the default 15 minutes is set.
131153
// Minimum frequency is 15 min. Android will automatically change your frequency to 15 min if you have configured a lower frequency.
132154
frequency: Duration(hours: 1),

example/ios/Runner.xcodeproj/project.pbxproj

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
/* Begin PBXBuildFile section */
1010
1498D2341E8E89220040F4C2 /* GeneratedPluginRegistrant.m in Sources */ = {isa = PBXBuildFile; fileRef = 1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */; };
11+
235EE877270FAF0800D284BB /* Info-Release.plist in Resources */ = {isa = PBXBuildFile; fileRef = 235EE875270FAF0800D284BB /* Info-Release.plist */; };
12+
235EE878270FAF0800D284BB /* Info-Debug.plist in Resources */ = {isa = PBXBuildFile; fileRef = 235EE876270FAF0800D284BB /* Info-Debug.plist */; };
1113
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */; };
1214
74858FAF1ED2DC5600515810 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 74858FAE1ED2DC5600515810 /* AppDelegate.swift */; };
1315
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 97C146FA1CF9000F007C117D /* Main.storyboard */; };
@@ -45,6 +47,8 @@
4547
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = GeneratedPluginRegistrant.h; sourceTree = "<group>"; };
4648
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = GeneratedPluginRegistrant.m; sourceTree = "<group>"; };
4749
1D8E96D33387777E764CB962 /* Pods-Runner.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.release.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.release.xcconfig"; sourceTree = "<group>"; };
50+
235EE875270FAF0800D284BB /* Info-Release.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Info-Release.plist"; sourceTree = "<group>"; };
51+
235EE876270FAF0800D284BB /* Info-Debug.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "Info-Debug.plist"; sourceTree = "<group>"; };
4852
24469BF6D0EF2BB3720EC4B4 /* Pods-RunnerTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-RunnerTests.release.xcconfig"; path = "Target Support Files/Pods-RunnerTests/Pods-RunnerTests.release.xcconfig"; sourceTree = "<group>"; };
4953
3B3967151E833CAA004F5970 /* AppFrameworkInfo.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = AppFrameworkInfo.plist; path = Flutter/AppFrameworkInfo.plist; sourceTree = "<group>"; };
5054
57B0832593D65C5496C91850 /* Pods-Runner.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Runner.debug.xcconfig"; path = "Target Support Files/Pods-Runner/Pods-Runner.debug.xcconfig"; sourceTree = "<group>"; };
@@ -60,7 +64,6 @@
6064
97C146FB1CF9000F007C117D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
6165
97C146FD1CF9000F007C117D /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
6266
97C147001CF9000F007C117D /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = "<group>"; };
63-
97C147021CF9000F007C117D /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
6467
9EA9C43326E8F58700E77F3E /* RunnerTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = RunnerTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
6568
9EA9C43526E8F58700E77F3E /* WorkmanagerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WorkmanagerTests.swift; sourceTree = "<group>"; };
6669
9EA9C43726E8F58700E77F3E /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
@@ -136,10 +139,11 @@
136139
97C146F01CF9000F007C117D /* Runner */ = {
137140
isa = PBXGroup;
138141
children = (
142+
235EE876270FAF0800D284BB /* Info-Debug.plist */,
143+
235EE875270FAF0800D284BB /* Info-Release.plist */,
139144
97C146FA1CF9000F007C117D /* Main.storyboard */,
140145
97C146FD1CF9000F007C117D /* Assets.xcassets */,
141146
97C146FF1CF9000F007C117D /* LaunchScreen.storyboard */,
142-
97C147021CF9000F007C117D /* Info.plist */,
143147
97C146F11CF9000F007C117D /* Supporting Files */,
144148
1498D2321E8E86230040F4C2 /* GeneratedPluginRegistrant.h */,
145149
1498D2331E8E89220040F4C2 /* GeneratedPluginRegistrant.m */,
@@ -231,7 +235,7 @@
231235
TargetAttributes = {
232236
97C146ED1CF9000F007C117D = {
233237
CreatedOnToolsVersion = 7.3.1;
234-
DevelopmentTeam = GPGRWN6G4J;
238+
DevelopmentTeam = 6KRGLYTFWP;
235239
LastSwiftMigration = 1110;
236240
};
237241
9EA9C43226E8F58700E77F3E = {
@@ -267,8 +271,10 @@
267271
buildActionMask = 2147483647;
268272
files = (
269273
97C147011CF9000F007C117D /* LaunchScreen.storyboard in Resources */,
274+
235EE877270FAF0800D284BB /* Info-Release.plist in Resources */,
270275
3B3967161E833CAA004F5970 /* AppFrameworkInfo.plist in Resources */,
271276
97C146FE1CF9000F007C117D /* Assets.xcassets in Resources */,
277+
235EE878270FAF0800D284BB /* Info-Debug.plist in Resources */,
272278
97C146FC1CF9000F007C117D /* Main.storyboard in Resources */,
273279
);
274280
runOnlyForDeploymentPostprocessing = 0;
@@ -643,7 +649,7 @@
643649
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
644650
CLANG_ENABLE_MODULES = YES;
645651
CURRENT_PROJECT_VERSION = "$(FLUTTER_BUILD_NUMBER)";
646-
DEVELOPMENT_TEAM = GPGRWN6G4J;
652+
DEVELOPMENT_TEAM = 6KRGLYTFWP;
647653
ENABLE_BITCODE = NO;
648654
FRAMEWORK_SEARCH_PATHS = (
649655
"$(inherited)",

example/ios/Runner/AppDelegate.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import workmanager
1919
// This will make other plugins available during a background operation.
2020
GeneratedPluginRegistrant.register(with: registry)
2121
}
22+
23+
WorkmanagerPlugin.registerTask(withIdentifier: "vn.newwave.custom-task-identifier")
2224

2325
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
2426

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3+
<plist version="1.0">
4+
<dict>
5+
<key>NSLocalNetworkUsageDescription</key>
6+
<string>Allows Flutter tools to connect and debug your application</string>
7+
<key>NSBonjourServices</key>
8+
<array>
9+
<string>_dartobservatory._tcp</string>
10+
</array>
11+
<key>BGTaskSchedulerPermittedIdentifiers</key>
12+
<array>
13+
<string>workmanager.background.task</string>
14+
<string>vn.newwave.custom-task-identifier</string>
15+
</array>
16+
<key>CFBundleDevelopmentRegion</key>
17+
<string>$(DEVELOPMENT_LANGUAGE)</string>
18+
<key>CFBundleExecutable</key>
19+
<string>$(EXECUTABLE_NAME)</string>
20+
<key>CFBundleIdentifier</key>
21+
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
22+
<key>CFBundleInfoDictionaryVersion</key>
23+
<string>6.0</string>
24+
<key>CFBundleName</key>
25+
<string>workmanager_example</string>
26+
<key>CFBundlePackageType</key>
27+
<string>APPL</string>
28+
<key>CFBundleShortVersionString</key>
29+
<string>1.0</string>
30+
<key>CFBundleSignature</key>
31+
<string>????</string>
32+
<key>CFBundleVersion</key>
33+
<string>1.0</string>
34+
<key>LSRequiresIPhoneOS</key>
35+
<true/>
36+
<key>UIBackgroundModes</key>
37+
<array>
38+
<string>fetch</string>
39+
<string>processing</string>
40+
</array>
41+
<key>UILaunchStoryboardName</key>
42+
<string>LaunchScreen</string>
43+
<key>UIMainStoryboardFile</key>
44+
<string>Main</string>
45+
<key>UISupportedInterfaceOrientations</key>
46+
<array>
47+
<string>UIInterfaceOrientationPortrait</string>
48+
<string>UIInterfaceOrientationLandscapeLeft</string>
49+
<string>UIInterfaceOrientationLandscapeRight</string>
50+
</array>
51+
<key>UISupportedInterfaceOrientations~ipad</key>
52+
<array>
53+
<string>UIInterfaceOrientationPortrait</string>
54+
<string>UIInterfaceOrientationPortraitUpsideDown</string>
55+
<string>UIInterfaceOrientationLandscapeLeft</string>
56+
<string>UIInterfaceOrientationLandscapeRight</string>
57+
</array>
58+
<key>UIViewControllerBasedStatusBarAppearance</key>
59+
<false/>
60+
</dict>
61+
</plist>

example/ios/Runner/Info.plist renamed to example/ios/Runner/Info-Release.plist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<key>BGTaskSchedulerPermittedIdentifiers</key>
66
<array>
77
<string>workmanager.background.task</string>
8+
<string>vn.newwave.custom-task-identifier</string>
89
</array>
910
<key>CFBundleDevelopmentRegion</key>
1011
<string>$(DEVELOPMENT_LANGUAGE)</string>

example/lib/main.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ class _MyAppState extends State<MyApp> {
126126
child: Text("Perform a BG Task"),
127127
onPressed: () {
128128
Workmanager().registerOneOffTask(
129-
"1",
129+
"vn.newwave.custom-task-identifier",
130130
simpleTaskKey,
131131
inputData: <String, dynamic>{
132132
'int': 1,
@@ -150,7 +150,7 @@ class _MyAppState extends State<MyApp> {
150150
child: Text("Register OneOff Task"),
151151
onPressed: () {
152152
Workmanager().registerOneOffTask(
153-
"1",
153+
"vn.newwave.custom-task-identifier",
154154
simpleTaskKey,
155155
inputData: <String, dynamic>{
156156
'int': 1,

ios/Classes/SwiftWorkmanagerPlugin.swift

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,8 @@ extension String {
1010
}
1111

1212
public class SwiftWorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate {
13-
1413
static let identifier = "be.tramckrijte.workmanager"
1514

16-
static let defaultBGProcessingTaskIdentifier = "workmanager.background.task"
17-
1815
private static var flutterPluginRegistrantCallback: FlutterPluginRegistrantCallback?
1916

2017
private struct ForegroundMethodChannel {
@@ -31,6 +28,7 @@ public class SwiftWorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate {
3128
struct RegisterOneOffTask {
3229
static let name = "\(RegisterOneOffTask.self)".lowercasingFirst
3330
enum Arguments: String {
31+
case uniqueName
3432
case initialDelaySeconds
3533
case networkType
3634
case requiresCharging
@@ -52,7 +50,7 @@ public class SwiftWorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate {
5250
}
5351

5452
@available(iOS 13.0, *)
55-
private func handleBGProcessingTask(_ task: BGProcessingTask) {
53+
private static func handleBGProcessingTask(_ task: BGProcessingTask) {
5654
let operationQueue = OperationQueue()
5755

5856
// Create an operation that performs the main part of the background task
@@ -77,20 +75,18 @@ public class SwiftWorkmanagerPlugin: FlutterPluginAppLifeCycleDelegate {
7775
operationQueue.addOperation(operation)
7876
}
7977

80-
public override func application(_ application: UIApplication,
81-
didFinishLaunchingWithOptions launchOptions: [AnyHashable: Any] = [:]) -> Bool {
78+
@objc
79+
public static func registerTask(withIdentifier identifier: String) {
8280
if #available(iOS 13.0, *) {
8381
BGTaskScheduler.shared.register(
84-
forTaskWithIdentifier: SwiftWorkmanagerPlugin.defaultBGProcessingTaskIdentifier,
82+
forTaskWithIdentifier: identifier,
8583
using: nil
8684
) { task in
8785
if let task = task as? BGProcessingTask {
88-
self.handleBGProcessingTask(task)
86+
handleBGProcessingTask(task)
8987
}
9088
}
9189
}
92-
93-
return true
9490
}
9591
}
9692

@@ -156,8 +152,13 @@ extension SwiftWorkmanagerPlugin: FlutterPlugin {
156152
result(WMPError.invalidParameters.asFlutterError)
157153
return
158154
}
155+
guard let identifier =
156+
arguments[method.Arguments.uniqueName.rawValue] as? String else {
157+
result(WMPError.invalidParameters.asFlutterError)
158+
return
159+
}
159160
let request = BGProcessingTaskRequest(
160-
identifier: SwiftWorkmanagerPlugin.defaultBGProcessingTaskIdentifier
161+
identifier: identifier
161162
)
162163
let requiresCharging = arguments[method.Arguments.requiresCharging.rawValue] as? Bool ?? false
163164

ios/Classes/WorkmanagerPlugin.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#import <Flutter/Flutter.h>
22

33
@interface WorkmanagerPlugin : NSObject<FlutterPlugin>
4+
5+
+ (void)registerTaskWithIdentifier:(NSString *) taskIdentifier;
6+
47
@end

ios/Classes/WorkmanagerPlugin.m

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,10 @@ + (void)setPluginRegistrantCallback:(FlutterPluginRegistrantCallback)callback {
1616
[SwiftWorkmanagerPlugin setPluginRegistrantCallback:callback];
1717
}
1818

19+
+ (void)registerTaskWithIdentifier:(NSString *) taskIdentifier {
20+
if (@available(iOS 13, *)) {
21+
[SwiftWorkmanagerPlugin registerTaskWithIdentifier:taskIdentifier];
22+
}
23+
}
24+
1925
@end

0 commit comments

Comments
 (0)