-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApp.js
101 lines (84 loc) · 2.98 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
import React, { useState, useEffect, useRef } from 'react';
import * as Notifications from "expo-notifications";
import { StatusBar } from 'expo-status-bar';
import { WebView } from 'react-native-webview';
import registerForPushNotificationsAsync from './lib/pushNotifications';
import { StyleSheet, BackHandler } from 'react-native';
import Constants from "expo-constants";
import handleExternalLinks from './lib/handleExternalLinks';
import injectCustomJavaScript from './lib/injectCustomJavaScript.js';
// matches the background color of the webapp's navbar
const navbarStaticTopColor = "rgba(39,151,175,0.9)";
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: false,
shouldSetBadge: false,
}),
});
const baseUrl = () => {
console.log('extra.baseUrl =', Constants.expoConfig.extra.baseUrl);
// While the staging and production profiles define a BASE_URL we need a
// default for development
return Constants.expoConfig.extra.baseUrl || "https://staging.timeoverflow.org";
};
export default function App() {
const [currentUrl, setCurrentUrl] = useState(baseUrl());
const [expoPushToken, setExpoPushToken] = useState("");
const responseListener = useRef();
const webViewRef = useRef(null);
useEffect(() => {
const backAction = () => {
console.log("Back button pressed");
try {
webViewRef.current?.goBack();
} catch (err) {
console.log("[handleBackButtonPress] Error : ", err.message);
} finally {
return true; // eslint-disable-line no-unsafe-finally
}
};
const backHandler = BackHandler.addEventListener("hardwareBackPress", backAction);
return () => backHandler.remove();
}, []);
useEffect(() => {
const getToken = async () => {
const token = await registerForPushNotificationsAsync();
setExpoPushToken(token)
};
getToken();
responseListener.current =
Notifications.addNotificationResponseReceivedListener((response) => {
console.log('content =', response.notification.request.content);
const { url } = response.notification.request.content.data;
setCurrentUrl(`${baseUrl()}${url}`);
});
return () => {
Notifications.removeNotificationSubscription(responseListener.current);
};
}, []);
const handleStateChange = async (navState) => {
console.log('state change =', navState);
const { url } = navState;
setCurrentUrl(url);
injectCustomJavaScript(webViewRef, expoPushToken, url);
await handleExternalLinks(url, webViewRef);
};
return (
<>
<StatusBar style="light" backgroundColor={navbarStaticTopColor} />
<WebView
ref={(ref) => (webViewRef.current = ref)}
style={styles.container}
source={{ uri: currentUrl }}
scalesPageToFit={false}
onNavigationStateChange={handleStateChange}
/>
</>
);
}
const styles = StyleSheet.create({
container: {
marginTop: Constants.statusBarHeight,
},
});