-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.android.js
53 lines (44 loc) · 1.43 KB
/
index.android.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
// @flow
import { AppRegistry, NativeModules } from 'react-native'
import type { BackgroundTaskInterface } from './types'
const { AppState, BackgroundJob: RNBackgroundJob } = NativeModules
const JOB_KEY = 'backgroundTask'
const BackgroundTask: BackgroundTaskInterface = {
register: function(task, {
period = 9000000, // every 15 minutes
timeout = 30,
} = {}) {
// Cancel any existing tasks, as we can only have one to match iOS, and we
// have no way to tell whether the function has changed or not.
RNBackgroundJob.cancelAll()
// Register the headless task
const fn = async () => { task() }
AppRegistry.registerHeadlessTask(JOB_KEY, () => fn)
// Schedule it to run as a periodic task
AppState.getCurrentAppState(
({ appState }) => {
RNBackgroundJob.schedule(
JOB_KEY,
timeout,
period,
true, // persist after restart
RNBackgroundJob.ANY, // network type
false, // requires charging
false, // requires device idle
appState === 'active',//false, // alwaysRunning,
'', // notificationTitle,
'', // notificationIcon,
'' // notificationText
)
},
() => { console.error(`Can't get AppState`) }
)
},
cancel: function() {
RNBackgroundJob.cancelAll()
},
finish: function() {
// Needed for iOS, no-op on Android
},
}
module.exports = BackgroundTask