-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
stopAlarm.ts
130 lines (116 loc) · 3.61 KB
/
stopAlarm.ts
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
import * as TaskManager from "expo-task-manager";
import * as Notifications from "expo-notifications";
import throttle from "lodash.throttle";
import WebView from "react-native-webview";
import {
Accuracy,
requestBackgroundPermissionsAsync,
getBackgroundPermissionsAsync,
startLocationUpdatesAsync,
stopLocationUpdatesAsync,
} from "expo-location";
import { Platform } from "react-native";
import { AsyncAlert } from "./asyncAlert";
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true,
}),
});
const LOCATION_NOTIFICATION_TASK_NAME = "background-notification-location";
const targetLocation = {
latitude: 0,
longitude: 0,
stopId: "",
title: "Arrived",
body: "Here",
};
const setStopAlarm = async () => {
const { status: notificationStatus } = await Notifications.requestPermissionsAsync();
if ( notificationStatus !== "granted" ) {
await AsyncAlert("Fail to setup alarm without notification permission.");
return false;
}
const { status: bgStatus } = await getBackgroundPermissionsAsync();
if (bgStatus !== "granted") {
if ( Platform.OS === 'android' ) {
await AsyncAlert('Background location permission is required for setting up the arrival reminder')
}
const { status } = await requestBackgroundPermissionsAsync();
if ( status !== "granted" ) {
return false;
}
}
await startLocationUpdatesAsync(LOCATION_NOTIFICATION_TASK_NAME, {
accuracy: Accuracy.Balanced,
timeInterval: 5000,
deferredUpdatesTimeout: 5000,
});
return true;
};
export const toggleAlarm = async ({ stopId, title, body, lat, lng }: any) => {
if (targetLocation.stopId === stopId) {
targetLocation.latitude = 0;
targetLocation.longitude = 0;
targetLocation.stopId = "";
try {
stopLocationUpdatesAsync(LOCATION_NOTIFICATION_TASK_NAME);
} catch (e) {
}
return true
} else {
targetLocation.stopId = stopId;
targetLocation.title = title;
targetLocation.body = body;
targetLocation.latitude = lat;
targetLocation.longitude = lng;
return await setStopAlarm();
}
};
export const postAlarmToWebView = (webViewRef: React.RefObject<WebView>) => {
webViewRef?.current?.postMessage(
JSON.stringify({
type: "stop-alarm-stop-id",
value: targetLocation.stopId,
})
);
};
TaskManager.defineTask(
LOCATION_NOTIFICATION_TASK_NAME,
throttle(({ data: { locations }, error }: { data: any; error: any }) => {
if (error) {
return;
}
if (locations && Array.isArray(locations) && locations.length) {
if (getDistance(targetLocation, locations[0].coords) < 500) {
Notifications.scheduleNotificationAsync({
content: {
title: targetLocation.title,
body: targetLocation.body,
data: { data: "goes" },
},
trigger: null,
});
toggleAlarm(targetLocation);
try {
stopLocationUpdatesAsync(LOCATION_NOTIFICATION_TASK_NAME);
} catch (e) {
}
}
return;
}
}, 500)
);
const getDistance = (a: any, b: any) => {
const R = 6371e3; // metres
const φ1 = (a.latitude * Math.PI) / 180; // φ, λ in radians
const φ2 = (b.latitude * Math.PI) / 180;
const Δφ = ((b.latitude - a.latitude) * Math.PI) / 180;
const Δλ = ((b.longitude - a.longitude) * Math.PI) / 180;
const aa =
Math.sin(Δφ / 2) * Math.sin(Δφ / 2) +
Math.cos(φ1) * Math.cos(φ2) * Math.sin(Δλ / 2) * Math.sin(Δλ / 2);
const c = 2 * Math.atan2(Math.sqrt(aa), Math.sqrt(1 - aa));
return R * c; // in metres
};