-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.tsx
112 lines (97 loc) · 3.14 KB
/
App.tsx
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
import { StyleSheet, Text, View, PermissionsAndroid,Platform ,Button, Alert, SafeAreaView} from 'react-native'
import React, {useEffect,useRef } from 'react';
import Geolocation from 'react-native-geolocation-service';
import ReactNativeForegroundService from "@supersami/rn-foreground-service";
const App = () => {
const watchIdRef = useRef(null);
if (Platform.OS === 'android') {
// Use PermissionsAndroid here
}
useEffect(() => {
requestLocationPermission()
updateforeground();
Notification()
startTracking();
}, []);
const requestLocationPermission = async () => {
Geolocation.requestAuthorization('always')
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: 'Location Permission',
message: 'App needs access to your location.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log('1Location permission granted');
} else {
console.log('Location permission denied');
}
PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_BACKGROUND_LOCATION,
{
title: 'Background Location Permission',
message:
'We need access to your location ' +
'so you can get live quality updates.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
} catch (err) {
console.warn(err);
}
}
const updateforeground =()=>{
ReactNativeForegroundService.add_task(() => startTracking(), {
delay: 100,
onLoop: true,
taskId: "taskid",
onError: (e) => console.log(`Error logging:`, e),
});
}
const Notification =()=>{
ReactNativeForegroundService.start({
id: 1244,
title: 'Location Tracking',
message: 'Location Tracking',
icon: 'ic_launcher',
button: false,
button2: false,
// buttonText: "Button",
// button2Text: "Anther Button",
// buttonOnPress: "cray",
setOnlyAlertOnce: true,
color: '#000000',
});
startTracking()
}
const startTracking = async () => {
let s= Geolocation.requestAuthorization('always');
Geolocation.watchPosition(
position => {
let coordinates: any = [];
coordinates[0] = position.coords.longitude;
coordinates[1] = position.coords.latitude;
console.warn(Platform.OS,"App Position tracking",coordinates)
},
error => {
console.log('maperror in getting location', error.code, error.message);
},
{ enableHighAccuracy: true, distanceFilter: 0 },
);
};
return (
<View style={{justifyContent:'center',alignItems:'center',flex:1}}>
<Text style={{color:'red',fontWeight:'600',fontSize:20,margin:30}}>Location Tracking</Text>
{Platform.OS==="android" && <Button onPress={Notification} title='Start Tracking'/>}
</View>
)
}
export default App
const styles = StyleSheet.create({})