forked from nahidmbstu/React-Native-Foreground-Service-Demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
110 lines (98 loc) · 2.52 KB
/
App.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
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React from 'react';
import {
StyleSheet,
View,
Button,
Platform,
PermissionsAndroid,
AppRegistry,
Alert,
} from 'react-native';
import {Colors} from 'react-native/Libraries/NewAppScreen';
import Geolocation from 'react-native-geolocation-service';
import ForegroundService from 'react-native-foreground-service';
let foregroundTask = async (params) => {
console.log(params);
// take your device location and send to the server
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: ' App Need Location Permission',
message: 'we need this to track your status',
buttonPositive: 'OK',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
Geolocation.watchPosition(
(position) => {
console.log(position);
},
(error) => console.log(error),
{
enableHighAccuracy: true,
timeout: 15000,
maximumAge: 10000,
distanceFilter: 1,
},
);
} else {
Alert.alert('permission not given');
}
} catch (e) {
console.error(e);
}
};
AppRegistry.registerHeadlessTask('location', () => foregroundTask);
const App = () => {
React.useEffect(() => {}, []);
const create = async () => {
if ((Platform.OS == 'android', Platform.Version >= 26)) {
let notificationConfig = {
id: 3,
title: 'Conveyence location service',
message: `Conveyence location service`,
visibility: 'public',
importance: 'low',
number: String(1),
};
await ForegroundService.startService(notificationConfig);
await ForegroundService.runTask({
taskName: 'location',
delay: 10000,
});
} else {
Alert.alert('Platform not Supported');
}
};
const stop = async () => {
// await VIForegroundService.stopService();
await ForegroundService.stopServiceAll();
// stop service when no longer needed
};
return (
<View style={styles.scrollView}>
<Button title={'Start Location Service'} onPress={create} />
<View style={styles.div} />
<Button title={'Stop Location Service'} onPress={stop} />
</View>
);
};
const styles = StyleSheet.create({
scrollView: {
backgroundColor: Colors.lighter,
padding: 10,
},
div: {
width: '100%',
height: 10,
},
});
export default App;