Skip to content

Commit 6b24406

Browse files
committed
[iOS] iOS no longer requires manual setup in AppDelegate.m for registering custom tasks with [fetchManager registerBGProcessingTask]
1 parent d345f9b commit 6b24406

File tree

9 files changed

+46
-53
lines changed

9 files changed

+46
-53
lines changed

CHANGELOG.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
## 0.5.0 - 2020-01-03
1+
## 0.5.1 - 2020-02-19
2+
3+
## Minor Breaking Change for iOS Setup
4+
5+
* [iOS] It's no longer necessary to `registerBGProcessingTask` in `AppDelegate.m` for tasks registered for use with `#scheduleTask`. The SDK now reads the App `.plist` and automatically registers those tasks found in *"Permitted background task scheduler identifiers"*. Remove **all** code in your `AppDelegate.m` that references `TSBackgroundFetch`.
6+
![](https://dl.dropboxusercontent.com/s/t5xfgah2gghqtws/ios-setup-permitted-identifiers.png?dl=1)
7+
8+
9+
## 0.5.0 - 2020-02-03
210
* [Added] [Android] New option `forceAlarmManager` for bypassing `JobScheduler` mechanism in favour of `AlarmManager` for more precise scheduling task execution.
311
* [Changed] Migrate iOS deprecated "background-fetch" API to new [BGTaskScheduler](https://developer.apple.com/documentation/backgroundtasks/bgtaskscheduler?language=objc). See new required steps in iOS Setup.
412
* [Added] Added new `BackgroundFetch.scheduleTask` method for scheduling custom "onehot" and periodic tasks in addition to the default fetch-task.

README.md

+18-14
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ By [**Transistor Software**](http://transistorsoft.com), creators of [**Flutter
88

99
Background Fetch is a *very* simple plugin which will awaken an app in the background about **every 15 minutes**, providing a short period of background running-time. This plugin will execute your provided `callbackFn` whenever a background-fetch event occurs.
1010

11+
:new: Background Fetch now provides a [__`scheduleTask`__](#executing-custom-tasks) method for scheduling arbitrary "one-shot" or periodic tasks.
12+
1113
### iOS
12-
There is **no way** to increase the rate which a fetch-event occurs and this plugin sets the rate to the most frequent possible — you will **never** receive an event faster than **15 minutes**. The operating-system will automatically throttle the rate the background-fetch events occur based upon usage patterns. Eg: if user hasn't turned on their phone for a long period of time, fetch events will occur less frequently.
14+
- There is **no way** to increase the rate which a fetch-event occurs and this plugin sets the rate to the most frequent possible — you will **never** receive an event faster than **15 minutes**. The operating-system will automatically throttle the rate the background-fetch events occur based upon usage patterns. Eg: if user hasn't turned on their phone for a long period of time, fetch events will occur less frequently.
15+
- [__`scheduleTask`__](#executing-custom-tasks) seems only to fire when the device is plugged into power.
1316

1417
### Android
15-
The Android plugin provides a [Headless](https://pub.dartlang.org/documentation/background_fetch/latest/background_fetch/BackgroundFetchConfig/enableHeadless.html) implementation allowing you to continue handling events even after app-termination.
18+
- The Android plugin provides a [Headless](https://pub.dartlang.org/documentation/background_fetch/latest/background_fetch/BackgroundFetchConfig/enableHeadless.html) implementation allowing you to continue handling events even after app-termination.
19+
1620

1721
# Contents
1822

@@ -29,7 +33,7 @@ The Android plugin provides a [Headless](https://pub.dartlang.org/documentation/
2933

3034
```yaml
3135
dependencies:
32-
background_fetch: '^0.5.0'
36+
background_fetch: '^0.5.1'
3337
```
3438
3539
### Or latest from Git:
@@ -214,7 +218,7 @@ BackgroundFetch.configure(BackgroundFetchConfig(
214218
215219
// Use a switch statement to route task-handling.
216220
switch (taskId) {
217-
case 'com.foo.customtask':
221+
case 'com.transistorsoft.customtask':
218222
print("Received custom task");
219223
break;
220224
default:
@@ -224,9 +228,9 @@ BackgroundFetch.configure(BackgroundFetchConfig(
224228
BackgroundFetch.finish(taskId);
225229
});
226230
227-
// Step 2: Schedule a custom "oneshot" task "com.foo.customtask" to execute 5000ms from now.
231+
// Step 2: Schedule a custom "oneshot" task "com.transistorsoft.customtask" to execute 5000ms from now.
228232
BackgroundFetch.scheduleTask(TaskConfig(
229-
taskId: "com.foo.customtask",
233+
taskId: "com.transistorsoft.customtask",
230234
delay: 5000 // <-- milliseconds
231235
));
232236
```
@@ -235,22 +239,22 @@ BackgroundFetch.scheduleTask(TaskConfig(
235239

236240
### iOS
237241

238-
#### New `BGTaskScheduler` API for iOS 13+
239-
- The old command *Debug->Simulate Background Fetch* no longer works with new `BGTaskSCheduler` API.
240-
- At the time of writing, the new task simulator does not yet work in Simulator; Only real devices.
242+
#### :new: `BGTaskScheduler` API for iOS 13+
243+
244+
- :warning: At the time of writing, the new task simulator does not yet work in Simulator; Only real devices.
241245
- See Apple docs [Starting and Terminating Tasks During Development](https://developer.apple.com/documentation/backgroundtasks/starting_and_terminating_tasks_during_development?language=objc)
242246
- After running your app in XCode, Click the `[||]` button to initiate a *Breakpoint*.
243-
244-
![](https://dl.dropboxusercontent.com/s/zr7w3g8ivf71u32/ios-simulate-bgtask-pause.png?dl=1)
245-
246247
- In the console `(lldb)`, paste the following command (**Note:** use cursor up/down keys to cycle through previously run commands):
247248
```obj-c
248249
e -l objc -- (void)[[BGTaskScheduler sharedScheduler] _simulateLaunchForTaskWithIdentifier:@"com.transistorsoft.fetch"]
249250
```
250-
![](https://dl.dropboxusercontent.com/s/87c9uctr1ka3s1e/ios-simulate-bgtask-paste.png?dl=1)
251-
252251
- Click the `[ > ]` button to continue. The task will execute and the Callback function provided to **`BackgroundFetch.configure`** will receive the event.
253252
253+
254+
![](https://dl.dropboxusercontent.com/s/zr7w3g8ivf71u32/ios-simulate-bgtask-pause.png?dl=1)
255+
256+
![](https://dl.dropboxusercontent.com/s/87c9uctr1ka3s1e/ios-simulate-bgtask-paste.png?dl=1)
257+
254258
![](https://dl.dropboxusercontent.com/s/bsv0avap5c2h7ed/ios-simulate-bgtask-play.png?dl=1)
255259
256260
#### Old `BackgroundFetch` API

example/ios/Runner/AppDelegate.m

+1-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@ - (BOOL)application:(UIApplication *)application
88
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
99
[GeneratedPluginRegistrant registerWithRegistry:self];
1010
// Override point for customization after application launch.
11-
TSBackgroundFetch *fetch = [TSBackgroundFetch sharedInstance];
12-
[fetch registerBGProcessingTask:@"com.transistorsoft.customtask"];
13-
11+
1412
return [super application:application didFinishLaunchingWithOptions:launchOptions];
1513
}
1614

example/lib/main.dart

+5-2
Original file line numberDiff line numberDiff line change
@@ -103,16 +103,18 @@ class _MyAppState extends State<MyApp> {
103103
});
104104
});
105105

106+
// Schedule a "one-shot" custom-task in 10000ms.
107+
// These are fairly reliable on Android (particularly with forceAlarmManager) but not iOS,
108+
// where device must be powered (and delay will be throttled by the OS).
106109
BackgroundFetch.scheduleTask(TaskConfig(
107110
taskId: "com.transistorsoft.customtask",
108-
delay: 5000,
111+
delay: 10000,
109112
periodic: false,
110113
forceAlarmManager: true,
111114
stopOnTerminate: false,
112115
enableHeadless: true
113116
));
114117

115-
116118
// Optionally query the current BackgroundFetch status.
117119
int status = await BackgroundFetch.status;
118120
setState(() {
@@ -137,6 +139,7 @@ class _MyAppState extends State<MyApp> {
137139
prefs.setString(EVENTS_KEY, jsonEncode(_events));
138140

139141
if (taskId == "flutter_background_fetch") {
142+
// Schedule a one-shot task when fetch event received (for testing).
140143
BackgroundFetch.scheduleTask(TaskConfig(
141144
taskId: "com.transistorsoft.customtask",
142145
delay: 5000,

help/INSTALL-IOS.md

+5-26
Original file line numberDiff line numberDiff line change
@@ -11,42 +11,21 @@
1111

1212

1313
## Configure `Info.plist`
14-
1. Open your `Info.plist` and the key *"Permitted background task scheduler identifiers"*
14+
1. Open your __`Info.plist`__ and add the key *"Permitted background task scheduler identifiers"*
1515

1616
![](https://dl.dropboxusercontent.com/s/t5xfgah2gghqtws/ios-setup-permitted-identifiers.png?dl=1)
1717

1818
2. Add the **required identifier `com.transistorsoft.fetch`**.
1919

2020
![](https://dl.dropboxusercontent.com/s/kwdio2rr256d852/ios-setup-permitted-identifiers-add.png?dl=1)
2121

22-
3. If you intend to execute your own custom tasks via **`BackgroundFetch.scheduleTask`**, you must add those custom identifiers as well. For example, if you intend to execute a custom **`taskId: 'com.foo.customtask'`**, you must add the identifier **`com.foo.customtask`** to your *"Permitted background task scheduler identifiers"*, as well.
22+
3. If you intend to execute your own [custom tasks](#executing-custom-tasks) via **`BackgroundFetch.scheduleTask`**, you must add those custom identifiers as well. For example, if you intend to execute a custom **`taskId: 'com.transistorsoft.customtask'`**, you must add the identifier **`com.transistorsoft.customtask`** to your *"Permitted background task scheduler identifiers"*, as well.
23+
24+
:warning: A task identifier can be any string you wish, but it's a good idea to prefix them now with `com.transistorsoft.` &mdash; In the future, the `com.transistorsoft` prefix **may become required**.
2325

2426
```dart
2527
BackgroundFetch.scheduleTask(TaskConfig(
26-
taskId: 'com.foo.customtask',
28+
taskId: 'com.transistorsoft.customtask',
2729
delay: 60 * 60 * 1000 // In one hour (milliseconds)
2830
));
2931
```
30-
31-
## `AppDelegate.m`
32-
33-
**If** you added custom *Background Processing* identifier(s) in your `Info.plist` and intend to use **`BackgroundFetch.scheduleTask`**, you must register those custom identifier(s) in your **`AppDelegate`** method **`didFinishLaunchingWithOptions`**:
34-
35-
__Note:__ The SDK *automatically* registers its required fetch-task **`com.transistorsoft.fetch`** &mdash; You need only register **your own** custom task idenfifiers here.
36-
37-
```obj-c
38-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
39-
[GeneratedPluginRegistrant registerWithRegistry:self];
40-
.
41-
.
42-
.
43-
// [BackgroundFetch] Register your custom Background Processing task(s)
44-
TSBackgroundFetch *fetch = [TSBackgroundFetch sharedInstance];
45-
[fetch registerBGProcessingTask:@"com.foo.customtask"];
46-
.
47-
.
48-
.
49-
return [super application:application didFinishLaunchingWithOptions:launchOptions];
50-
}
51-
52-
```

ios/Classes/BackgroundFetchPlugin.m

+5-6
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@ @implementation BackgroundFetchPlugin {
2828
-(BOOL)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
2929
{
3030
NSLog(@"BackgroundFetch AppDelegate received fetch event");
31-
TSBackgroundFetch *fetchManager = [TSBackgroundFetch sharedInstance];
32-
[fetchManager performFetchWithCompletionHandler:completionHandler applicationState:application.applicationState];
31+
[[TSBackgroundFetch sharedInstance] performFetchWithCompletionHandler:completionHandler applicationState:application.applicationState];
3332
return YES;
3433
}
3534

3635
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
37-
[[TSBackgroundFetch sharedInstance] registerAppRefreshTask];
36+
[[TSBackgroundFetch sharedInstance] didFinishLaunching];
3837
return YES;
3938
}
4039

@@ -118,11 +117,11 @@ -(void) start:(FlutterResult)result {
118117
-(void) stop:(NSString*)taskId result:(FlutterResult)result {
119118
TSBackgroundFetch *fetchManager = [TSBackgroundFetch sharedInstance];
120119
if (!taskId) {
120+
// Remove fetch listener.
121121
[fetchManager removeListener:PLUGIN_ID];
122-
[fetchManager stop:nil];
123-
} else {
124-
[fetchManager stop:taskId];
125122
}
123+
// Calling stop with nil taskId will cause TSBackgroundFetch to stop all custom-tasks from #scheduleTask
124+
[fetchManager stop:taskId];
126125
[self status:result];
127126
}
128127

ios/TSBackgroundFetch.framework/Headers/TSBackgroundFetch.h

+2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
@property (nonatomic) BOOL stopOnTerminate;
1616
@property (readonly) BOOL configured;
1717
@property (readonly) BOOL active;
18+
@property (readonly) NSString *fetchTaskId;
1819

1920
+ (TSBackgroundFetch *)sharedInstance;
2021

22+
-(void) didFinishLaunching;
2123
-(void) registerAppRefreshTask;
2224
-(void) registerBGProcessingTask:(NSString*)identifier;
2325

Binary file not shown.

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: background_fetch
22
description: Periodic callbacks in the background for both iOS and Android. Includes Android Headless implementation.
3-
version: 0.5.0
3+
version: 0.5.1
44
homepage: https://github.com/transistorsoft/flutter_background_fetch
55

66
environment:

0 commit comments

Comments
 (0)