forked from vikeri/react-native-background-job
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
222 lines (216 loc) · 7.48 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
"use strict";
import {
NativeModules,
AppRegistry,
Platform,
DeviceEventEmitter
} from "react-native";
const AppState = NativeModules.AppState;
const tag = "BackgroundJob:";
const jobModule = Platform.select({
ios: {},
android: NativeModules.BackgroundJob
});
const nativeJobs = Platform.select({
ios: { jobs: {} },
android: jobModule.jobs
});
var jobs = {};
var globalWarning = __DEV__;
const BackgroundJob = {
NETWORK_TYPE_UNMETERED: jobModule.UNMETERED,
NETWORK_TYPE_NONE: jobModule.NONE,
NETWORK_TYPE_ANY: jobModule.ANY,
/**
* Registers jobs and the functions they should run.
*
* This has to run on each initialization of React Native and it has to run in the global scope and not inside any
* component life cycle methods. See example project. Only registering the job will not start running the job.
* It has to be scheduled by `schedule` to start running.
*
* @param {Object} obj
* @param {string} obj.jobKey A unique key for the job
* @param {function} obj.job The JS-function that will be run
*
* @example
* import BackgroundJob from 'react-native-background-job';
*
* const backgroundJob = {
* jobKey: "myJob",
* job: () => console.log("Running in background")
* };
*
* BackgroundJob.register(backgroundJob);
*
*/
register: function({ jobKey, job }) {
const existingJob = jobs[jobKey];
if (!existingJob || !existingJob.registered) {
var fn = async () => {
job();
};
AppRegistry.registerHeadlessTask(jobKey, () => fn);
DeviceEventEmitter.addListener("RNBackgroundJob", job);
if (existingJob) {
jobs[jobKey].registered = true;
} else {
const scheduledJob = nativeJobs.filter(nJob => nJob.jobKey == jobKey);
const scheduled = scheduledJob[0] != undefined;
jobs[jobKey] = { registered: true, scheduled, job };
}
}
},
/**
* Schedules a new job.
*
* This only has to be run once while `register` has to be run on each initialization of React Native.
*
* @param {Object} obj
* @param {string} obj.jobKey A unique key for the job
* @param {number} obj.timeout How long the JS job may run before being terminated by Android (in ms).
* @param {number} [obj.period = 900000] - The frequency to run the job with (in ms). This number is not exact, Android may modify it to save batteries. Note: For Android > N, the minimum is 900 0000 (15 min).
* @param {boolean} [obj.persist = true] If the job should persist over a device restart.
* @param {boolean} [obj.warn = true] If a warning should be raised if overwriting a job that was already scheduled.
* @param {number} [obj.networkType = BackgroundJob.NETWORK_TYPE_NONE] Only run for specific network requirements, (not respected by pre Android N devices) [docs](https://developer.android.com/reference/android/app/job/JobInfo.html#NETWORK_TYPE_ANY)
* @param {boolean} [obj.requiresCharging = false] Only run job when device is charging, (not respected by pre Android N devices) [docs](https://developer.android.com/reference/android/app/job/JobInfo.Builder.html#setRequiresCharging(boolean))
* @param {boolean} [obj.requiresDeviceIdle = false] Only run job when the device is idle, (not respected by pre Android N devices) [docs](https://developer.android.com/reference/android/app/job/JobInfo.Builder.html#setRequiresDeviceIdle(boolean))
* @param {boolean} [obj.alwaysRunning = false] Creates a foreground service that will keep the app alive forever. Suitable for music playback etc. Will always show a notification.
* @param {string} obj.notificationTitle The title of the persistent notification when `alwaysRunning`
* @param {string} obj.notificationText The text of the persistent notification when `alwaysRunning`
* @param {string} obj.notificationIcon The icon string (in drawable) of the persistent notification when `alwaysRunning`
*
* @example
* import BackgroundJob from 'react-native-background-job';
*
* const backgroundJob = {
* jobKey: "myJob",
* job: () => console.log("Running in background")
* };
*
* BackgroundJob.register(backgroundJob);
*
* var backgroundSchedule = {
* jobKey: "myJob",
* timeout: 5000
* }
*
* BackgroundJob.schedule(backgroundSchedule);
*/
schedule: function({
jobKey,
timeout,
period = 900000,
persist = true,
warn = true,
networkType = this.NETWORK_TYPE_NONE,
requiresCharging = false,
requiresDeviceIdle = false,
alwaysRunning = false,
notificationTitle,
notificationText,
notificationIcon
}) {
const savedJob = jobs[jobKey];
if (!savedJob) {
console.error(
`${tag} The job ${jobKey} has not been registered, you must register it before you can schedule it.`
);
} else {
if (savedJob.scheduled && warn && globalWarning) {
console.warn(`${tag} Overwriting background job: ${jobKey}`);
} else {
jobs[jobKey].scheduled = true;
}
jobModule.schedule(
jobKey,
timeout,
period,
persist,
networkType,
requiresCharging,
requiresDeviceIdle,
alwaysRunning,
notificationTitle,
notificationIcon,
notificationText
);
}
},
/**
* Fetches all the currently scheduled jobs
*
* @param {Object} obj
* @param {function(Array)} obj.callback A list of all the scheduled jobs will be passed to the callback
*
* @example
* import BackgroundJob from 'react-native-background-job';
*
* BackgroundJob.getAll({callback: (jobs) => console.log("Jobs:",jobs)});
*
*/
getAll: function({ callback }) {
jobModule.getAll(callback);
},
/**
* Cancel a specific job
*
* @param {Object} obj
* @param {string} obj.jobKey The unique key for the job
* @param {boolean} [obj.warn = true] If one tries to cancel a job that has not been scheduled it will warn
*
* @example
* import BackgroundJob from 'react-native-background-job';
*
* BackgroundJob.cancel({jobKey: 'myJob'});
*/
cancel: function({ jobKey, warn = true }) {
if (warn && globalWarning && (!jobs[jobKey] || !jobs[jobKey].scheduled)) {
console.warn(
`${tag} Trying to cancel the job ${jobKey} but it is not scheduled`
);
}
jobModule.cancel(jobKey);
jobs[jobKey] ? (jobs[jobKey].scheduled = false) : null;
},
/**
* Cancels all the scheduled jobs
*
* @example
* import BackgroundJob from 'react-native-background-job';
*
* BackgroundJob.cancelAll();
*/
cancelAll: function() {
jobModule.cancelAll();
const keys = Object.keys(jobs);
keys.map(key => {
jobs[key].scheduled = false;
});
},
/**
* Sets the global warning level
*
* @param {boolean} warn
*
* @example
* import BackgroundJob from 'react-native-background-job';
*
* BackgroundJob.setGlobalWarnings(false);
*
*/
setGlobalWarnings: function(warn) {
globalWarning = warn;
}
};
if (Platform.OS == "ios") {
Object.keys(BackgroundJob).map(v => {
BackgroundJob[v] = () => {
if (globalWarning) {
console.warn(
"react-native-background-job is not available on iOS yet. See https://github.com/vikeri/react-native-background-job#supported-platforms"
);
}
};
});
}
module.exports = BackgroundJob;