@ammarahmed/react-native-background-fetch 4.2.1 → 4.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/android/src/main/java/com/transistorsoft/rnbackgroundfetch/HeadlessTask.java +98 -61
- package/android/src/main/java/com/transistorsoft/tsbackgroundfetch/BGTask.java +18 -4
- package/android/src/main/java/com/transistorsoft/tsbackgroundfetch/BackgroundFetch.java +0 -0
- package/android/src/main/java/com/transistorsoft/tsbackgroundfetch/BackgroundFetchConfig.java +0 -0
- package/android/src/main/java/com/transistorsoft/tsbackgroundfetch/BootReceiver.java +0 -0
- package/android/src/main/java/com/transistorsoft/tsbackgroundfetch/FetchAlarmReceiver.java +0 -0
- package/android/src/main/java/com/transistorsoft/tsbackgroundfetch/FetchJobService.java +0 -0
- package/android/src/main/java/com/transistorsoft/tsbackgroundfetch/LifecycleManager.java +0 -0
- package/index.d.ts +183 -176
- package/package.json +1 -1
|
@@ -4,7 +4,12 @@ import android.content.Context;
|
|
|
4
4
|
import android.os.Handler;
|
|
5
5
|
import android.util.Log;
|
|
6
6
|
|
|
7
|
+
import androidx.annotation.NonNull;
|
|
8
|
+
import androidx.annotation.Nullable;
|
|
9
|
+
|
|
10
|
+
import com.facebook.infer.annotation.Assertions;
|
|
7
11
|
import com.facebook.react.ReactApplication;
|
|
12
|
+
import com.facebook.react.ReactInstanceEventListener;
|
|
8
13
|
import com.facebook.react.ReactInstanceManager;
|
|
9
14
|
import com.facebook.react.ReactNativeHost;
|
|
10
15
|
import com.facebook.react.bridge.ReactContext;
|
|
@@ -13,50 +18,34 @@ import com.facebook.react.bridge.WritableMap;
|
|
|
13
18
|
import com.facebook.react.bridge.WritableNativeMap;
|
|
14
19
|
import com.facebook.react.jstasks.HeadlessJsTaskConfig;
|
|
15
20
|
import com.facebook.react.jstasks.HeadlessJsTaskContext;
|
|
16
|
-
|
|
21
|
+
|
|
17
22
|
import com.transistorsoft.tsbackgroundfetch.BGTask;
|
|
18
23
|
import com.transistorsoft.tsbackgroundfetch.BackgroundFetch;
|
|
19
24
|
import com.facebook.react.common.LifecycleState;
|
|
20
25
|
|
|
26
|
+
import java.lang.reflect.Method;
|
|
27
|
+
|
|
21
28
|
/**
|
|
22
29
|
* Created by chris on 2018-01-17.
|
|
23
30
|
*/
|
|
24
31
|
|
|
25
|
-
public class HeadlessTask
|
|
26
|
-
private static String HEADLESS_TASK_NAME = "BackgroundFetch";
|
|
27
|
-
private static Handler mHandler = new Handler();
|
|
28
|
-
private ReactNativeHost mReactNativeHost;
|
|
29
|
-
private HeadlessJsTaskContext mActiveTaskContext;
|
|
32
|
+
public class HeadlessTask {
|
|
33
|
+
private static final String HEADLESS_TASK_NAME = "BackgroundFetch";
|
|
34
|
+
private static final Handler mHandler = new Handler();
|
|
30
35
|
|
|
36
|
+
private final BGTask mBGTask;
|
|
31
37
|
public HeadlessTask(Context context, BGTask task) {
|
|
32
|
-
|
|
33
|
-
ReactApplication reactApplication = ((ReactApplication) context.getApplicationContext());
|
|
34
|
-
mReactNativeHost = reactApplication.getReactNativeHost();
|
|
35
|
-
} catch (AssertionError | ClassCastException e) {
|
|
36
|
-
Log.e(BackgroundFetch.TAG, "Failed to fetch ReactApplication. Task ignored.");
|
|
37
|
-
return; // <-- Do nothing. Just return
|
|
38
|
-
}
|
|
38
|
+
mBGTask = task;
|
|
39
39
|
WritableMap clientEvent = new WritableNativeMap();
|
|
40
40
|
clientEvent.putString("taskId", task.getTaskId());
|
|
41
41
|
clientEvent.putBoolean("timeout", task.getTimedOut());
|
|
42
42
|
HeadlessJsTaskConfig config = new HeadlessJsTaskConfig(HEADLESS_TASK_NAME, clientEvent, 30000);
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (mActiveTaskContext != null) {
|
|
48
|
-
mActiveTaskContext.removeTaskEventListener(this);
|
|
43
|
+
try {
|
|
44
|
+
startTask(config, context);
|
|
45
|
+
} catch (AssertionError e) {
|
|
46
|
+
Log.d(BackgroundFetch.TAG, "[HeadlessTask] Failed invoke HeadlessTask: " + e.getMessage());
|
|
49
47
|
}
|
|
50
48
|
}
|
|
51
|
-
@Override
|
|
52
|
-
public void onHeadlessJsTaskStart(int taskId) {
|
|
53
|
-
Log.d(BackgroundFetch.TAG,"onHeadlessJsTaskStart: " + taskId);
|
|
54
|
-
}
|
|
55
|
-
@Override
|
|
56
|
-
public void onHeadlessJsTaskFinish(int taskId) {
|
|
57
|
-
Log.d(BackgroundFetch.TAG, "onHeadlessJsTaskFinish: " + taskId);
|
|
58
|
-
mActiveTaskContext.removeTaskEventListener(this);
|
|
59
|
-
}
|
|
60
49
|
|
|
61
50
|
/**
|
|
62
51
|
* Start a task. This method handles starting a new React instance if required.
|
|
@@ -65,27 +54,13 @@ public class HeadlessTask implements HeadlessJsTaskEventListener {
|
|
|
65
54
|
*
|
|
66
55
|
* @param taskConfig describes what task to start and the parameters to pass to it
|
|
67
56
|
*/
|
|
68
|
-
protected void startTask(final HeadlessJsTaskConfig taskConfig) {
|
|
57
|
+
protected void startTask(final HeadlessJsTaskConfig taskConfig, Context context) {
|
|
69
58
|
UiThreadUtil.assertOnUiThread();
|
|
70
|
-
|
|
71
|
-
ReactContext reactContext =
|
|
59
|
+
|
|
60
|
+
ReactContext reactContext = getReactContext(context);
|
|
61
|
+
|
|
72
62
|
if (reactContext == null) {
|
|
73
|
-
|
|
74
|
-
@Override
|
|
75
|
-
public void onReactContextInitialized(final ReactContext reactContext) {
|
|
76
|
-
// Hack to fix unknown problem executing asynchronous BackgroundTask when ReactContext is created *first time*. Fixed by adding short delay before #invokeStartTask
|
|
77
|
-
mHandler.postDelayed(new Runnable() {
|
|
78
|
-
@Override
|
|
79
|
-
public void run() {
|
|
80
|
-
invokeStartTask(reactContext, taskConfig);
|
|
81
|
-
}
|
|
82
|
-
}, 500);
|
|
83
|
-
reactInstanceManager.removeReactInstanceEventListener(this);
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
if (!reactInstanceManager.hasStartedCreatingInitialContext()) {
|
|
87
|
-
reactInstanceManager.createReactContextInBackground();
|
|
88
|
-
}
|
|
63
|
+
createReactContextAndScheduleTask(taskConfig, context);
|
|
89
64
|
} else {
|
|
90
65
|
invokeStartTask(reactContext, taskConfig);
|
|
91
66
|
}
|
|
@@ -96,24 +71,86 @@ public class HeadlessTask implements HeadlessJsTaskEventListener {
|
|
|
96
71
|
return;
|
|
97
72
|
}
|
|
98
73
|
final HeadlessJsTaskContext headlessJsTaskContext = HeadlessJsTaskContext.getInstance(reactContext);
|
|
99
|
-
|
|
100
|
-
|
|
74
|
+
|
|
75
|
+
UiThreadUtil.runOnUiThread(() -> {
|
|
76
|
+
try {
|
|
77
|
+
final int taskId = headlessJsTaskContext.startTask(taskConfig);
|
|
78
|
+
Log.d(BackgroundFetch.TAG, "[HeadlessTask] start taskId: " + taskId);
|
|
79
|
+
// Add a BGTask.finish(taskId) listener. This is executed when the user runs BackgroundFetch.finish(taskId).
|
|
80
|
+
// We use this to finish the RN headless task.
|
|
81
|
+
mBGTask.setCompletionHandler(() -> {
|
|
82
|
+
Log.d(BackgroundFetch.TAG, "[HeadlessTask] end taskId: " + taskId);
|
|
83
|
+
if (headlessJsTaskContext.isTaskRunning(taskId)) {
|
|
84
|
+
headlessJsTaskContext.finishTask(taskId);
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
} catch (IllegalStateException exception) {
|
|
88
|
+
Log.e(BackgroundFetch.TAG, "[HeadlessTask] task attempted to run in the foreground. Task ignored.");
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
private ReactNativeHost getReactNativeHost(Context context) {
|
|
94
|
+
return ((ReactApplication) context.getApplicationContext()).getReactNativeHost();
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Get the {ReactHost} used by this app. ure and returns null if not.
|
|
99
|
+
*/
|
|
100
|
+
private @Nullable Object getReactHost(Context context) {
|
|
101
|
+
context = context.getApplicationContext();
|
|
101
102
|
try {
|
|
102
|
-
|
|
103
|
+
Method getReactHost = context.getClass().getMethod("getReactHost");
|
|
104
|
+
return getReactHost.invoke(context);
|
|
105
|
+
// Original non-reflection return:
|
|
106
|
+
//return ((ReactApplication) context.getApplicationContext()).getReactHost();
|
|
107
|
+
} catch (Exception e) {
|
|
108
|
+
Log.d(BackgroundFetch.TAG, "[HeadlessTask] Reflection error ReactHost: " + e);
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
private ReactContext getReactContext(Context context) {
|
|
114
|
+
|
|
115
|
+
Object reactHost = getReactHost(context);
|
|
116
|
+
Assertions.assertNotNull(reactHost, "getReactHost() is null in New Architecture");
|
|
117
|
+
try {
|
|
118
|
+
Method getCurrentReactContext = reactHost.getClass().getMethod("getCurrentReactContext");
|
|
119
|
+
return (ReactContext) getCurrentReactContext.invoke(reactHost);
|
|
120
|
+
} catch (Exception e) {
|
|
121
|
+
Log.e(BackgroundFetch.TAG, "[HeadlessTask] Reflection error getCurrentReactContext: " + e);
|
|
122
|
+
return null;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
private void createReactContextAndScheduleTask(final HeadlessJsTaskConfig taskConfig, Context context) {
|
|
127
|
+
Log.d(BackgroundFetch.TAG, "[HeadlessTask] initializing ReactContext");
|
|
128
|
+
|
|
129
|
+
final Object reactHost = getReactHost(context);
|
|
130
|
+
|
|
131
|
+
ReactInstanceEventListener callback = new ReactInstanceEventListener() {
|
|
103
132
|
@Override
|
|
104
|
-
public void
|
|
133
|
+
public void onReactContextInitialized(@NonNull ReactContext reactContext) {
|
|
134
|
+
mHandler.postDelayed(() -> invokeStartTask(reactContext, taskConfig), 500);
|
|
105
135
|
try {
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
136
|
+
Method removeReactInstanceEventListener = reactHost.getClass().getMethod("removeReactInstanceEventListener", ReactInstanceEventListener.class);
|
|
137
|
+
removeReactInstanceEventListener.invoke(reactHost, this);
|
|
138
|
+
} catch (Exception e) {
|
|
139
|
+
Log.e(BackgroundFetch.TAG, "[HeadlessTask] reflection error removeReactInstanceEventListener" + e);
|
|
110
140
|
}
|
|
111
141
|
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
try {
|
|
145
|
+
Method addReactInstanceEventListener = reactHost.getClass().getMethod("addReactInstanceEventListener", ReactInstanceEventListener.class);
|
|
146
|
+
addReactInstanceEventListener.invoke(reactHost, callback);
|
|
147
|
+
Method startReactHost = reactHost.getClass().getMethod("start");
|
|
148
|
+
startReactHost.invoke(reactHost);
|
|
149
|
+
} catch (Exception e) {
|
|
150
|
+
Log.e(BackgroundFetch.TAG, "[HeadlessTask] reflection error addReactInstanceEventListener: " + e);
|
|
151
|
+
}
|
|
117
152
|
|
|
118
153
|
}
|
|
119
|
-
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
}
|
|
@@ -65,6 +65,7 @@ public class BGTask {
|
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
+
private final List<FetchJobService.CompletionHandler> mCompletionHandlers = new ArrayList<>();
|
|
68
69
|
private FetchJobService.CompletionHandler mCompletionHandler;
|
|
69
70
|
private String mTaskId;
|
|
70
71
|
private int mJobId;
|
|
@@ -73,7 +74,8 @@ public class BGTask {
|
|
|
73
74
|
|
|
74
75
|
BGTask(final Context context, String taskId, FetchJobService.CompletionHandler handler, int jobId) {
|
|
75
76
|
mTaskId = taskId;
|
|
76
|
-
mCompletionHandler = handler;
|
|
77
|
+
//mCompletionHandler = handler;
|
|
78
|
+
mCompletionHandlers.add(handler);
|
|
77
79
|
mJobId = jobId;
|
|
78
80
|
|
|
79
81
|
mTimeoutTask = new Runnable() {
|
|
@@ -96,18 +98,30 @@ public class BGTask {
|
|
|
96
98
|
return ((mTaskId != null) && mTaskId.equalsIgnoreCase(taskId));
|
|
97
99
|
}
|
|
98
100
|
|
|
99
|
-
void setCompletionHandler(FetchJobService.CompletionHandler handler) {
|
|
100
|
-
|
|
101
|
+
public void setCompletionHandler(FetchJobService.CompletionHandler handler) {
|
|
102
|
+
synchronized (mCompletionHandlers) {
|
|
103
|
+
mCompletionHandlers.add(handler);
|
|
104
|
+
}
|
|
105
|
+
//mCompletionHandler = handler;
|
|
101
106
|
}
|
|
102
107
|
|
|
103
108
|
void finish() {
|
|
109
|
+
synchronized (mCompletionHandlers) {
|
|
110
|
+
for (FetchJobService.CompletionHandler handler : mCompletionHandlers) {
|
|
111
|
+
handler.finish();
|
|
112
|
+
}
|
|
113
|
+
mCompletionHandlers.clear();
|
|
114
|
+
}
|
|
115
|
+
/*
|
|
104
116
|
if (mCompletionHandler != null) {
|
|
105
117
|
mCompletionHandler.finish();
|
|
106
118
|
}
|
|
119
|
+
*/
|
|
107
120
|
if (mTimeoutTask != null) {
|
|
108
121
|
BackgroundFetch.getUiHandler().removeCallbacks(mTimeoutTask);
|
|
109
122
|
}
|
|
110
|
-
|
|
123
|
+
|
|
124
|
+
//mCompletionHandler = null;
|
|
111
125
|
removeTask(mTaskId);
|
|
112
126
|
}
|
|
113
127
|
|
|
File without changes
|
package/android/src/main/java/com/transistorsoft/tsbackgroundfetch/BackgroundFetchConfig.java
CHANGED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
package/index.d.ts
CHANGED
|
@@ -1,185 +1,192 @@
|
|
|
1
|
-
declare module "react-native-background-fetch" {
|
|
1
|
+
declare module "@ammarahmed/react-native-background-fetch" {
|
|
2
|
+
interface AbstractConfig {
|
|
3
|
+
/**
|
|
4
|
+
* [Android only] Set false to continue background-fetch events after user terminates the app. Default to true.
|
|
5
|
+
*/
|
|
6
|
+
stopOnTerminate?: boolean;
|
|
7
|
+
/**
|
|
8
|
+
* [Android only] Set true to initiate background-fetch events when the device is rebooted. Defaults to false.
|
|
9
|
+
*/
|
|
10
|
+
startOnBoot?: boolean;
|
|
11
|
+
/**
|
|
12
|
+
* [Android only] Set true to enable Headless mechanism for handling fetch events after app termination.
|
|
13
|
+
*/
|
|
14
|
+
enableHeadless?: boolean;
|
|
15
|
+
/**
|
|
16
|
+
* [Android only]
|
|
17
|
+
*/
|
|
18
|
+
forceAlarmManager?: boolean;
|
|
19
|
+
/**
|
|
20
|
+
* [Android only] Set detailed description of the kind of network your job requires.
|
|
21
|
+
*
|
|
22
|
+
* If your job doesn't need a network connection, you don't need to use this option, as the default is [[BackgroundFetch.NEWORK_TYPE_NONE]].
|
|
23
|
+
*
|
|
24
|
+
* Calling this method defines network as a strict requirement for your job. If the network requested is not available your job will never run.
|
|
25
|
+
*/
|
|
26
|
+
requiredNetworkType?: NetworkType;
|
|
27
|
+
/**
|
|
28
|
+
* [Android only] Specify that to run this job, the device's battery level must not be low.
|
|
29
|
+
*
|
|
30
|
+
* This defaults to false. If true, the job will only run when the battery level is not low, which is generally the point where the user is given a "low battery" warning.
|
|
31
|
+
*/
|
|
32
|
+
requiresBatteryNotLow?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* [Android only] Specify that to run this job, the device's available storage must not be low.
|
|
35
|
+
*
|
|
36
|
+
* This defaults to false. If true, the job will only run when the device is not in a low storage state, which is generally the point where the user is given a "low storage" warning.
|
|
37
|
+
*/
|
|
38
|
+
requiresStorageNotLow?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* [Android only] Specify that to run this job, the device must be charging (or be a non-battery-powered device connected to permanent power, such as Android TV devices). This defaults to false.
|
|
41
|
+
*/
|
|
42
|
+
requiresCharging?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* [Android only] When set true, ensure that this job will not run if the device is in active use.
|
|
45
|
+
*
|
|
46
|
+
* The default state is false: that is, the for the job to be runnable even when someone is interacting with the device.
|
|
47
|
+
*
|
|
48
|
+
* This state is a loose definition provided by the system. In general, it means that the device is not currently being used interactively, and has not been in use for some time. As such, it is a good time to perform resource heavy jobs. Bear in mind that battery usage will still be attributed to your application, and surfaced to the user in battery stats.
|
|
49
|
+
*/
|
|
50
|
+
requiresDeviceIdle?: boolean;
|
|
51
|
+
}
|
|
2
52
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
* [Android only] Set detailed description of the kind of network your job requires.
|
|
22
|
-
*
|
|
23
|
-
* If your job doesn't need a network connection, you don't need to use this option, as the default is [[BackgroundFetch.NEWORK_TYPE_NONE]].
|
|
24
|
-
*
|
|
25
|
-
* Calling this method defines network as a strict requirement for your job. If the network requested is not available your job will never run.
|
|
26
|
-
*/
|
|
27
|
-
requiredNetworkType?:NetworkType;
|
|
28
|
-
/**
|
|
29
|
-
* [Android only] Specify that to run this job, the device's battery level must not be low.
|
|
30
|
-
*
|
|
31
|
-
* This defaults to false. If true, the job will only run when the battery level is not low, which is generally the point where the user is given a "low battery" warning.
|
|
32
|
-
*/
|
|
33
|
-
requiresBatteryNotLow?:boolean;
|
|
34
|
-
/**
|
|
35
|
-
* [Android only] Specify that to run this job, the device's available storage must not be low.
|
|
36
|
-
*
|
|
37
|
-
* This defaults to false. If true, the job will only run when the device is not in a low storage state, which is generally the point where the user is given a "low storage" warning.
|
|
38
|
-
*/
|
|
39
|
-
requiresStorageNotLow?:boolean;
|
|
40
|
-
/**
|
|
41
|
-
* [Android only] Specify that to run this job, the device must be charging (or be a non-battery-powered device connected to permanent power, such as Android TV devices). This defaults to false.
|
|
42
|
-
*/
|
|
43
|
-
requiresCharging?:boolean;
|
|
44
|
-
/**
|
|
45
|
-
* [Android only] When set true, ensure that this job will not run if the device is in active use.
|
|
46
|
-
*
|
|
47
|
-
* The default state is false: that is, the for the job to be runnable even when someone is interacting with the device.
|
|
48
|
-
*
|
|
49
|
-
* This state is a loose definition provided by the system. In general, it means that the device is not currently being used interactively, and has not been in use for some time. As such, it is a good time to perform resource heavy jobs. Bear in mind that battery usage will still be attributed to your application, and surfaced to the user in battery stats.
|
|
50
|
-
*/
|
|
51
|
-
requiresDeviceIdle?:boolean;
|
|
52
|
-
}
|
|
53
|
+
interface TaskConfig extends AbstractConfig {
|
|
54
|
+
/**
|
|
55
|
+
* The name of the task. This will be used with [[BackgroundFetch.finish]] to signal task-completion.
|
|
56
|
+
*/
|
|
57
|
+
taskId: string;
|
|
58
|
+
/**
|
|
59
|
+
* The minimum interval in milliseconds to execute this task.
|
|
60
|
+
*/
|
|
61
|
+
delay: number;
|
|
62
|
+
/**
|
|
63
|
+
* Whether this task will continue executing or just a "one-shot".
|
|
64
|
+
*/
|
|
65
|
+
periodic?: boolean;
|
|
66
|
+
/**
|
|
67
|
+
* When set true, ensure that this job will run if the device has network connection.
|
|
68
|
+
*/
|
|
69
|
+
requiresNetworkConnectivity?: boolean;
|
|
70
|
+
}
|
|
53
71
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
* The minimum interval in milliseconds to execute this task.
|
|
61
|
-
*/
|
|
62
|
-
delay:number;
|
|
63
|
-
/**
|
|
64
|
-
* Whether this task will continue executing or just a "one-shot".
|
|
65
|
-
*/
|
|
66
|
-
periodic?:boolean;
|
|
67
|
-
/**
|
|
68
|
-
* When set true, ensure that this job will run if the device has network connection.
|
|
69
|
-
*/
|
|
70
|
-
requiresNetworkConnectivity?:boolean;
|
|
71
|
-
}
|
|
72
|
+
interface BackgroundFetchConfig extends AbstractConfig {
|
|
73
|
+
/**
|
|
74
|
+
* The minimum interval in minutes to execute background fetch events. Defaults to 15 minutes. Minimum is 15 minutes.
|
|
75
|
+
*/
|
|
76
|
+
minimumFetchInterval?: number;
|
|
77
|
+
}
|
|
72
78
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
+
/**
|
|
80
|
+
* | BackgroundFetchStatus | Description |
|
|
81
|
+
* |------------------------------------|-------------------------------------------------|
|
|
82
|
+
* | BackgroundFetch.STATUS_RESTRICTED | Background fetch updates are unavailable and the user cannot enable them again. For example, this status can occur when parental controls are in effect for the current user. |
|
|
83
|
+
* | BackgroundFetch.STATUS_DENIED | The user explicitly disabled background behavior for this app or for the whole system. |
|
|
84
|
+
* | BackgroundFetch.STATUS_AVAILABLE | Background fetch is available and enabled. |
|
|
85
|
+
*/
|
|
86
|
+
type BackgroundFetchStatus = 0 | 1 | 2;
|
|
79
87
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
+
/**
|
|
89
|
+
* | NetworkType | Description |
|
|
90
|
+
* |---------------------------------------|---------------------------------------------------------------|
|
|
91
|
+
* | BackgroundFetch.NETWORK_TYPE_NONE | This job doesn't care about network constraints, either any or none. |
|
|
92
|
+
* | BackgroundFetch.NETWORK_TYPE_ANY | This job requires network connectivity. |
|
|
93
|
+
* | BackgroundFetch.NETWORK_TYPE_CELLULAR | This job requires network connectivity that is a cellular network. |
|
|
94
|
+
* | BackgroundFetch.NETWORK_TYPE_UNMETERED | This job requires network connectivity that is unmetered. |
|
|
95
|
+
* | BackgroundFetch.NETWORK_TYPE_NOT_ROAMING | This job requires network connectivity that is not roaming. |
|
|
96
|
+
*/
|
|
97
|
+
type NetworkType = 0 | 1 | 2 | 3 | 4;
|
|
88
98
|
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
+
export interface HeadlessEvent {
|
|
100
|
+
/**
|
|
101
|
+
* The name of the task. This will be used with [[BackgroundFetch.finish]] to signal task-completion.
|
|
102
|
+
*/
|
|
103
|
+
taskId: string;
|
|
104
|
+
/**
|
|
105
|
+
* Whether this event is a timeout event or not. If true stop all processing and call [[BackgroundFetch.finish]] immediately.
|
|
106
|
+
*/
|
|
107
|
+
timeout: boolean;
|
|
108
|
+
}
|
|
99
109
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
+
/**
|
|
111
|
+
* BackgroundFetch is a module to receive periodic callbacks (min every 15 min) while your app is running in the background or terminated.
|
|
112
|
+
*/
|
|
113
|
+
export default class BackgroundFetch {
|
|
114
|
+
/**
|
|
115
|
+
* Background fetch updates are unavailable and the user cannot enable them again. For example, this status can occur when parental controls are in effect for the current user.
|
|
116
|
+
*/
|
|
117
|
+
static STATUS_RESTRICTED: BackgroundFetchStatus;
|
|
118
|
+
/**
|
|
119
|
+
* The user explicitly disabled background behavior for this app or for the whole system.
|
|
120
|
+
*/
|
|
121
|
+
static STATUS_DENIED: BackgroundFetchStatus;
|
|
122
|
+
/**
|
|
123
|
+
* Background fetch is available and enabled.
|
|
124
|
+
*/
|
|
125
|
+
static STATUS_AVAILABLE: BackgroundFetchStatus;
|
|
126
|
+
/**
|
|
127
|
+
* This job doesn't care about network constraints, either any or none.
|
|
128
|
+
*/
|
|
129
|
+
static NETWORK_TYPE_NONE: NetworkType;
|
|
130
|
+
/**
|
|
131
|
+
* This job requires network connectivity.
|
|
132
|
+
*/
|
|
133
|
+
static NETWORK_TYPE_ANY: NetworkType;
|
|
134
|
+
/**
|
|
135
|
+
* This job requires network connectivity that is a cellular network.
|
|
136
|
+
*/
|
|
137
|
+
static NETWORK_TYPE_CELLULAR: NetworkType;
|
|
138
|
+
/**
|
|
139
|
+
* This job requires network connectivity that is unmetered.
|
|
140
|
+
*/
|
|
141
|
+
static NETWORK_TYPE_UNMETERED: NetworkType;
|
|
142
|
+
/**
|
|
143
|
+
* This job requires network connectivity that is not roaming.
|
|
144
|
+
*/
|
|
145
|
+
static NETWORK_TYPE_NOT_ROAMING: NetworkType;
|
|
110
146
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
* Background fetch is available and enabled.
|
|
125
|
-
*/
|
|
126
|
-
static STATUS_AVAILABLE: BackgroundFetchStatus;
|
|
127
|
-
/**
|
|
128
|
-
* This job doesn't care about network constraints, either any or none.
|
|
129
|
-
*/
|
|
130
|
-
static NETWORK_TYPE_NONE: NetworkType;
|
|
131
|
-
/**
|
|
132
|
-
* This job requires network connectivity.
|
|
133
|
-
*/
|
|
134
|
-
static NETWORK_TYPE_ANY: NetworkType;
|
|
135
|
-
/**
|
|
136
|
-
* This job requires network connectivity that is a cellular network.
|
|
137
|
-
*/
|
|
138
|
-
static NETWORK_TYPE_CELLULAR: NetworkType;
|
|
139
|
-
/**
|
|
140
|
-
* This job requires network connectivity that is unmetered.
|
|
141
|
-
*/
|
|
142
|
-
static NETWORK_TYPE_UNMETERED: NetworkType;
|
|
143
|
-
/**
|
|
144
|
-
* This job requires network connectivity that is not roaming.
|
|
145
|
-
*/
|
|
146
|
-
static NETWORK_TYPE_NOT_ROAMING: NetworkType;
|
|
147
|
+
/**
|
|
148
|
+
* Initial configuration of BackgroundFetch, including config-options and Fetch-callback. The [[start]] method will automatically be executed.
|
|
149
|
+
*/
|
|
150
|
+
static configure(
|
|
151
|
+
config: BackgroundFetchConfig,
|
|
152
|
+
onEvent: (taskId: string) => void,
|
|
153
|
+
onTimeout?: (taskId: string) => void
|
|
154
|
+
): Promise<BackgroundFetchStatus>;
|
|
155
|
+
/**
|
|
156
|
+
* Add an extra fetch event listener in addition to the one initially provided to [[configure]].
|
|
157
|
+
* @event
|
|
158
|
+
*/
|
|
159
|
+
static scheduleTask(config: TaskConfig): Promise<boolean>;
|
|
147
160
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
static status(callback?:(status:BackgroundFetchStatus) => void):Promise<BackgroundFetchStatus>;
|
|
180
|
-
/**
|
|
181
|
-
* [Android only] Register a function to execute when the app is terminated. Requires stopOnTerminate: false.
|
|
182
|
-
*/
|
|
183
|
-
static registerHeadlessTask(task:(event:HeadlessEvent) => Promise<void>):void;
|
|
184
|
-
}
|
|
161
|
+
/**
|
|
162
|
+
* Start subscribing to fetch events.
|
|
163
|
+
*/
|
|
164
|
+
static start(): Promise<BackgroundFetchStatus>;
|
|
165
|
+
/**
|
|
166
|
+
* Stop subscribing to fetch events.
|
|
167
|
+
*/
|
|
168
|
+
static stop(taskId?: string): Promise<boolean>;
|
|
169
|
+
/**
|
|
170
|
+
* You must execute [[finish]] within your fetch-callback to signal completion of your task.
|
|
171
|
+
*/
|
|
172
|
+
static finish(taskId?: string): void;
|
|
173
|
+
/**
|
|
174
|
+
* Query the BackgroundFetch API status
|
|
175
|
+
*
|
|
176
|
+
* | BackgroundFetchStatus | Description |
|
|
177
|
+
* |------------------------------------|-------------------------------------------------|
|
|
178
|
+
* | BackgroundFetch.STATUS_RESTRICTED | Background fetch updates are unavailable and the user cannot enable them again. For example, this status can occur when parental controls are in effect for the current user. |
|
|
179
|
+
* | BackgroundFetch.STATUS_DENIED | The user explicitly disabled background behavior for this app or for the whole system. |
|
|
180
|
+
* | BackgroundFetch.STATUS_AVAILABLE | Background fetch is available and enabled. |
|
|
181
|
+
*/
|
|
182
|
+
static status(
|
|
183
|
+
callback?: (status: BackgroundFetchStatus) => void
|
|
184
|
+
): Promise<BackgroundFetchStatus>;
|
|
185
|
+
/**
|
|
186
|
+
* [Android only] Register a function to execute when the app is terminated. Requires stopOnTerminate: false.
|
|
187
|
+
*/
|
|
188
|
+
static registerHeadlessTask(
|
|
189
|
+
task: (event: HeadlessEvent) => Promise<void>
|
|
190
|
+
): void;
|
|
191
|
+
}
|
|
185
192
|
}
|