forked from transistorsoft/rn-background-geolocation-demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
60 lines (47 loc) · 2.05 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
import { AppRegistry } from 'react-native';
import App from './src/App';
import BackgroundGeolocation from "./src/react-native-background-geolocation";
// Make BackgroundGeolocation API global for handy access in Javascript Debugger console
global.BackgroundGeolocation = BackgroundGeolocation;
import BackgroundFetch from "react-native-background-fetch";
AppRegistry.registerComponent('BGGeolocation', () => App);
/**
* BackgroundGeolocation Headless JS task.
* For more information, see: https://github.com/transistorsoft/react-native-background-geolocation/wiki/Android-Headless-Mode
*/
let BackgroundGeolocationHeadlessTask = async (event) => {
let params = event.params;
console.log('[BackgroundGeolocation HeadlessTask] -', event.name, params);
switch (event.name) {
case 'heartbeat':
// Use await for async tasks
/* DISABLED
let location = await BackgroundGeolocation.getCurrentPosition({
samples: 1,
persist: false
});
console.log('[BackgroundGeolocation HeadlessTask] - getCurrentPosition:', location);
*/
break;
}
}
BackgroundGeolocation.registerHeadlessTask(BackgroundGeolocationHeadlessTask);
/**
* BackgroundFetch Headless JS Task.
* For more information, see: https://github.com/transistorsoft/react-native-background-fetch#config-boolean-enableheadless-false
*/
let BackgroundFetchHeadlessTask = async (event) => {
console.log('[BackgroundFetch HeadlessTask] start');
// Important: await asychronous tasks when using HeadlessJS.
/* DISABLED
let location = await BackgroundGeolocation.getCurrentPosition({persist: false, samples: 1});
console.log('- current position: ', location);
// Required: Signal to native code that your task is complete.
// If you don't do this, your app could be terminated and/or assigned
// battery-blame for consuming too much time in background.
*/
console.log('[BackgroundFetch HeadlessTask] finished');
BackgroundFetch.finish();
}
// Register your BackgroundFetch HeadlessTask
BackgroundFetch.registerHeadlessTask(BackgroundFetchHeadlessTask);