-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
157 lines (129 loc) · 3.8 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow strict-local
*/
import React, {useEffect, useState} from 'react';
//import type {Node} from 'react';
import {
SafeAreaView,
StatusBar,
Text,
PermissionsAndroid,
DeviceEventEmitter,
Button,
} from 'react-native';
import Geolocation from 'react-native-geolocation-service';
import ReactNativeForegroundService from '@supersami/rn-foreground-service';
import { TestWatcher } from '@jest/core';
import { blockStatement } from '@babel/types';
const App= () => {
const [currentLongitude, setCurrentLongitude] = useState(0);
const [currentLatitude, setCurrentLatitude] = useState(0);
const eventEmitter = () => {
// device event emitter used to
let subscription = DeviceEventEmitter.addListener(
'notificationClickHandle',
function (e) {
if(e.button==='stop') {
onStop();
}
},
);
return function cleanup() {
subscription.remove();
};
}
useEffect(() => {
checkPermissions();
eventEmitter();
}, []);
const checkPermissions = async () => {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: 'App',
message: 'App access to your location',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
//START GEO SERVICE!!!
onStart();
} else {
// setLocationGranted(false);
alert("Location permission denied");
}
} catch (err) {
console.warn(err);
}
};
const getOneTimeLocation = () => {
Geolocation.getCurrentPosition(
//Will give you the current location
position => {
console.log(position.coords);
//Setting Longitude state
//Setting Longitude state
/* cause a stats error when the app was closed and opend again r*/
setCurrentLongitude(position.coords.longitude);
setCurrentLatitude(position.coords.latitude);
},
error => {
console.log(error.message);
},
{
enableHighAccuracy: true,
timeout: 30000,
maximumAge: 1000,
},
); };
const onStart = () => {
// Checking if the task i am going to create already exist and running, which means that the foreground is also running.
if (ReactNativeForegroundService.is_task_running('pcd')) return;
// Creating a task.
ReactNativeForegroundService.add_task(
() => getOneTimeLocation(),
{
delay: 30000,
onLoop: true,
taskId: 'taskid',
onError: (e) => console.log(`Error logging:`, e),
},
);
// starting foreground service.
return ReactNativeForegroundService.start({
id: 144,
title: 'SERVICE',
message: 'TASK IS RUNNING!!!',
importance: 'max',
number: String(1),
button: true,
buttonText: 'STOP',
buttonOnPress : 'stop',
mainOnPress : 'show',
});
};
const onStop = () => {
// Make always sure to remove the task before stoping the service. and instead of re-adding the task you can always update the task.
if (ReactNativeForegroundService.is_task_running('pcd')) {
ReactNativeForegroundService.remove_task('pcd');
}
// Stoping Foreground service.
return ReactNativeForegroundService.stop();
};
return (
<>
<StatusBar barStyle="dark-content" />
<SafeAreaView
style={{flex: 1, justifyContent: 'center', alignItems: 'center'}}>
<Text>{currentLatitude}-{currentLongitude}</Text>
<Button title={'Start'} onPress={onStart} />
<Button title={'Stop'} onPress={onStop} />
</SafeAreaView>
</>
);
};
export default App;