1
1
import React , { useState , useEffect , useRef } from 'react' ;
2
+ import * as Notifications from "expo-notifications" ;
2
3
import { StatusBar } from 'expo-status-bar' ;
3
4
import { WebView } from 'react-native-webview' ;
5
+ import registerForPushNotificationsAsync from './lib/pushNotifications' ;
4
6
import { StyleSheet , Text , View , BackHandler } from 'react-native' ;
5
7
import * as Updates from 'expo-updates' ;
6
8
9
+ Notifications . setNotificationHandler ( {
10
+ handleNotification : async ( ) => ( {
11
+ shouldShowAlert : true ,
12
+ shouldPlaySound : false ,
13
+ shouldSetBadge : false ,
14
+ } ) ,
15
+ } ) ;
16
+
7
17
const baseUrl = ( ) => {
8
18
const { releaseChannel } = Updates ;
9
19
console . log ( 'releaseChannel =' , releaseChannel ) ;
@@ -15,6 +25,8 @@ const baseUrl = () => {
15
25
16
26
export default function App ( ) {
17
27
const [ currentUrl , setCurrentUrl ] = useState ( baseUrl ( ) ) ;
28
+ const [ expoPushToken , setExpoPushToken ] = useState ( "" ) ;
29
+ const responseListener = useRef ( ) ;
18
30
19
31
const webViewRef = useRef ( null ) ;
20
32
@@ -36,14 +48,50 @@ export default function App() {
36
48
return ( ) => backHandler . remove ( ) ;
37
49
} , [ ] ) ;
38
50
51
+ useEffect ( ( ) => {
52
+ registerForPushNotificationsAsync ( ) . then ( ( token ) =>
53
+ setExpoPushToken ( token )
54
+ ) ;
55
+
56
+ responseListener . current =
57
+ Notifications . addNotificationResponseReceivedListener ( ( response ) => {
58
+ console . log ( 'content =' , response . notification . request . content ) ;
59
+ const { url } = response . notification . request . content . data ;
60
+ setCurrentUrl ( `${ baseUrl ( ) } ${ url } ` ) ;
61
+ } ) ;
62
+
63
+ return ( ) => {
64
+ Notifications . removeNotificationSubscription ( responseListener . current ) ;
65
+ } ;
66
+ } , [ ] ) ;
67
+
68
+ const handleLoggedInPage = ( webview , token ) => {
69
+ webview ?. injectJavaScript (
70
+ `window.TimeOverflowRegisterExpoDeviceToken(\'${ token } \');`
71
+ ) ;
72
+ } ;
73
+
74
+ const injectCustomJavaScript = ( url ) => {
75
+ if ( / m e m b e r s / . test ( url ) ) {
76
+ handleLoggedInPage ( webViewRef . current , expoPushToken ) ;
77
+ }
78
+ } ;
79
+
80
+ const handleStateChange = ( navState ) => {
81
+ console . log ( 'state change =' , navState ) ;
82
+ const { url } = navState ;
83
+ setCurrentUrl ( url ) ;
84
+ injectCustomJavaScript ( url ) ;
85
+ } ;
86
+
39
87
return (
40
88
< >
41
89
< WebView
42
90
ref = { ( ref ) => ( webViewRef . current = ref ) }
43
91
style = { styles . container }
44
92
source = { { uri : currentUrl } }
45
93
scalesPageToFit = { false }
46
- // onNavigationStateChange={handleStateChange}
94
+ onNavigationStateChange = { handleStateChange }
47
95
/>
48
96
</ >
49
97
) ;
0 commit comments