-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (77 loc) · 3.02 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
/**
* @format
*/
import { AppRegistry } from 'react-native';
import 'react-native-gesture-handler';
import 'react-native-screens';
import 'react-native-safe-area-context';
import App from './App';
import BackgroundGeolocation from './Helpers/react-native-background-geolocation';
import BackgroundFetch from 'react-native-background-fetch';
import { name as appName } from './app.json';
AppRegistry.registerComponent(appName, () => App);
const BackgroundGeolocationHeadlessTask = async (event) => {
let params = event.params;
console.log('[BackgroundGeolocation HeadlessTask] -', event.name, params);
switch (event.name) {
case 'heartbeat':
/**
* Enable this block to execute #getCurrentPosition in headless heartbeat event (will consume more power)
*/
// Use await for async tasks
const location = await BackgroundGeolocation.getCurrentPosition({
samples: 2,
persist: true,
extras: {
event: 'heartbeat',
headless: true
}
});
console.log('[BackgroundGeolocation HeadlessTask] - getCurrentPosition:', location);
break;
case 'authorization':
BackgroundGeolocation.setConfig({
url:'https://tracker.transistorsoft.com/api/locations'
});
break;
}
}
BackgroundGeolocation.registerHeadlessTask(BackgroundGeolocationHeadlessTask);
/**
* BackgroundFetch Headless JS Task.
* For more information, see: https://github.com/transistorsoft/react-native-background-fetch#config-boolean-enableheadless-false
*/
const BackgroundFetchHeadlessTask = async (event) => {
console.log('[BackgroundFetch HeadlessTask] start', event.taskId);
if (event.taskId == 'react-native-background-fetch') {
const location = await BackgroundGeolocation.getCurrentPosition({
samples: 2,
extras: {
event: 'background-fetch',
headless: true
}
});
console.log('[BackgroundFetch] getCurrentPosition: ', location);
/*
await BackgroundFetch.scheduleTask({
taskId: 'com.transistorsoft.customtask',
delay: 5000,
stopOnTerminate: false,
enableHeadless: true,
forceAlarmManager: true
});
*/
}
// Important: await asychronous tasks when using HeadlessJS.
/* DISABLED
const 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(event.taskId);
}
// Register your BackgroundFetch HeadlessTask
BackgroundFetch.registerHeadlessTask(BackgroundFetchHeadlessTask);