Skip to content

Commit 467a2a2

Browse files
committed
Update docs
1 parent 3cbf035 commit 467a2a2

File tree

8 files changed

+53
-35
lines changed

8 files changed

+53
-35
lines changed

CHANGELOG.md

+22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
## 0.5.0 - 2020-01-03
2+
* [Added] [Android] New option `forceAlarmManager` for bypassing `JobScheduler` mechanism in favour of `AlarmManager` for more precise scheduling task execution.
23
* [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.
34
* [Added] Added new `BackgroundFetch.scheduleTask` method for scheduling custom "onehot" and periodic tasks in addition to the default fetch-task.
45
```dart
@@ -37,8 +38,29 @@ BackgroundFetch.configure(BackgroundFetchConfig(
3738
print("[BackgroundFetch] taskId: $taskId");
3839
BackgroundFetch.finish(taskId); // <-- [NEW] Provided taskId to #finish method.
3940
});
41+
```
42+
43+
And with the Headless Task, as well:
44+
```dart
45+
/// This "Headless Task" is run when app is terminated.
46+
void backgroundFetchHeadlessTask(String taskId) async { // <-- 1. Headless task receives String taskId
47+
print("[BackgroundFetch] Headless event received: $taskId");
48+
49+
BackgroundFetch.finish(taskId); // <-- 2. #finish with taskId here as well.
50+
}
51+
52+
void main() {
53+
// Enable integration testing with the Flutter Driver extension.
54+
// See https://flutter.io/testing/ for more info.
55+
runApp(new MyApp());
56+
57+
// Register to receive BackgroundFetch events after app is terminated.
58+
// Requires {stopOnTerminate: false, enableHeadless: true}
59+
BackgroundFetch.registerHeadlessTask(backgroundFetchHeadlessTask);
60+
}
4061
```
4162

63+
4264
## 0.4.0 - 2019-12-17
4365
* [Changed] Upgrade to new Flutter Plugin API "V2". Requires flutter sdk version 1.12. See [Upgrading pre 1.12 Android Projects](https://github.com/flutter/flutter/wiki/Upgrading-pre-1.12-Android-projects)
4466

example/android/app/src/main/java/com/transistorsoft/flutter/backgroundfetch/backgroundfetchexample/Application.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public class Application extends FlutterApplication {
1515
@Override
1616
public void onCreate() {
1717

18-
// Strict mode.
18+
/// Test Strict mode.
1919
/*
2020
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
2121
.detectDiskReads()
@@ -34,8 +34,10 @@ public void onCreate() {
3434

3535
super.onCreate();
3636

37-
Log.d("TSBackgroundFetch", "*********************** MainApplication");
38-
37+
///
38+
/// TEST onInitialized callback for custom MethodChannel
39+
///
40+
/*
3941
HeadlessTask.onInitialized(new HeadlessTask.OnInitializedCallback() {
4042
@Override
4143
public void onInitialized(FlutterEngine engine) {
@@ -49,10 +51,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result
4951
});
5052
}
5153
});
52-
53-
54-
//MethodChannel(engine.dartExecutor.binaryMessenger, CHANNEL).setMethodCallHandler { call, result ->
55-
// handleMethod(call, result, this)
54+
*/
5655

5756
}
5857
}

example/ios/Runner.xcodeproj/xcshareddata/xcschemes/Runner.xcscheme

+2-6
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
2828
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
2929
shouldUseLaunchSchemeArgsEnv = "YES">
30-
<Testables>
31-
</Testables>
3230
<MacroExpansion>
3331
<BuildableReference
3432
BuildableIdentifier = "primary"
@@ -38,8 +36,8 @@
3836
ReferencedContainer = "container:Runner.xcodeproj">
3937
</BuildableReference>
4038
</MacroExpansion>
41-
<AdditionalOptions>
42-
</AdditionalOptions>
39+
<Testables>
40+
</Testables>
4341
</TestAction>
4442
<LaunchAction
4543
buildConfiguration = "Debug"
@@ -61,8 +59,6 @@
6159
ReferencedContainer = "container:Runner.xcodeproj">
6260
</BuildableReference>
6361
</BuildableProductRunnable>
64-
<AdditionalOptions>
65-
</AdditionalOptions>
6662
</LaunchAction>
6763
<ProfileAction
6864
buildConfiguration = "Release"

example/lib/main.dart

+10-14
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,11 @@ import 'package:background_fetch/background_fetch.dart';
88

99
const EVENTS_KEY = "fetch_events";
1010

11-
const MethodChannel _methodChannel = const MethodChannel("channel_foo");
12-
1311

1412
/// This "Headless Task" is run when app is terminated.
1513
void backgroundFetchHeadlessTask(String taskId) async {
1614
print("[BackgroundFetch] Headless event received: $taskId");
1715
DateTime timestamp = DateTime.now();
18-
_methodChannel.invokeMethod('test');
1916

2017
SharedPreferences prefs = await SharedPreferences.getInstance();
2118

@@ -32,17 +29,16 @@ void backgroundFetchHeadlessTask(String taskId) async {
3229

3330
BackgroundFetch.finish(taskId);
3431

35-
/*
36-
BackgroundFetch.scheduleTask(TaskConfig(
37-
taskId: "foo",
38-
delay: 5000,
39-
periodic: false,
40-
forceAlarmManager: true,
41-
stopOnTerminate: false,
42-
enableHeadless: true
43-
));
44-
45-
*/
32+
if (taskId == 'flutter_background_fetch') {
33+
BackgroundFetch.scheduleTask(TaskConfig(
34+
taskId: "foo",
35+
delay: 5000,
36+
periodic: false,
37+
forceAlarmManager: true,
38+
stopOnTerminate: false,
39+
enableHeadless: true
40+
));
41+
}
4642
}
4743

4844
void main() {

help/INSTALL-IOS.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ __Note:__ The SDK *automatically* registers its required fetch-task **`com.trans
4242
.
4343
// [BackgroundFetch] Register your custom Background Processing task(s)
4444
TSBackgroundFetch *fetch = [TSBackgroundFetch sharedInstance];
45-
[fetch registerBackgroundProcessingTask:@"com.foo.customtask"];
45+
[fetch registerBGProcessingTask:@"com.foo.customtask"];
4646
.
4747
.
4848
.

ios/Classes/BackgroundFetchPlugin.m

+8-3
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ -(void) start:(FlutterResult)result {
101101

102102
[fetchManager status:^(UIBackgroundRefreshStatus status) {
103103
if (status == UIBackgroundRefreshStatusAvailable) {
104-
NSError *error = [fetchManager start:BACKGROUND_FETCH_TASK_ID];
104+
[fetchManager addListener:PLUGIN_ID callback:[self createCallback]];
105+
NSError *error = [fetchManager start:PLUGIN_ID];
105106
if (!error) {
106107
result(@(status));
107108
} else {
@@ -116,8 +117,12 @@ -(void) start:(FlutterResult)result {
116117

117118
-(void) stop:(NSString*)taskId result:(FlutterResult)result {
118119
TSBackgroundFetch *fetchManager = [TSBackgroundFetch sharedInstance];
119-
if (!taskId) { taskId = BACKGROUND_FETCH_TASK_ID; }
120-
[fetchManager stop:taskId];
120+
if (!taskId) {
121+
[fetchManager removeListener:PLUGIN_ID];
122+
[fetchManager stop:nil];
123+
} else {
124+
[fetchManager stop:taskId];
125+
}
121126
[self status:result];
122127
}
123128

Binary file not shown.

lib/background_fetch.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@ class _AbstractTaskConfig {
5252
/// import 'package:background_fetch/background_fetch.dart';
5353
///
5454
/// // This "Headless Task" is run when app is terminated.
55-
/// void backgroundFetchHeadlessTask() async {
56-
/// print('[BackgroundFetch] Headless event received.');
57-
/// BackgroundFetch.finish();
55+
/// void backgroundFetchHeadlessTask(String taskId) async {
56+
/// print("[BackgroundFetch] Headless event received: $taskId");
57+
/// BackgroundFetch.finish(taskId);
5858
/// }
5959
///
6060
/// void main() {
@@ -365,7 +365,7 @@ class BackgroundFetch {
365365
/// print('[BackgroundFetch] Event received.');
366366
/// // IMPORTANT: You must signal completion of your fetch task or the OS could punish your app for
367367
/// // spending much time in the background.
368-
/// BackgroundFetch.finish(BackgroundFetch.FETCH_RESULT_NEW_DATA);
368+
/// BackgroundFetch.finish(taskId);
369369
/// })
370370
/// ```
371371
static Future<int> configure(

0 commit comments

Comments
 (0)