forked from martijndeh/react-native-region-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
regionMonitor.android.js
52 lines (47 loc) · 1.38 KB
/
regionMonitor.android.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
import {AppRegistry, NativeModules, PermissionsAndroid} from 'react-native';
const {RNRegionMonitor} = NativeModules;
let callbacks = [];
AppRegistry.registerHeadlessTask("region-monitor-transition", () => {
return (geofenceEvent) => {
return new Promise((resolve, reject) => {
callbacks.forEach(callback => {
callback(geofenceEvent);
});
resolve();
});
}
});
const permissionsCheck = () => {
return PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION).then(permissionStatus => {
switch (permissionStatus) {
case true: // Pre-Marshmallow Android devices send true back
case PermissionsAndroid.RESULTS.GRANTED: {
return true;
}
case PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN:
case PermissionsAndroid.RESULTS.DENIED:
default: {
throw new Error("Cannot obtain permissions to perform geofencing");
}
}
});
};
export default {
addCircularRegion: (center, radius, id) => {
return permissionsCheck().then(() => {
return RNRegionMonitor.addCircularRegion(center, radius, id);
});
},
clearRegions: RNRegionMonitor.clearRegions,
removeCircularRegion: RNRegionMonitor.removeCircularRegion,
requestAuthorization: permissionsCheck,
onRegionChange: (callback) => {
callbacks.push(callback);
return function off() {
const idx = callbacks.indexOf(callback);
if(idx >= 0) {
callbacks.splice(i, 1);
}
};
},
}