File tree 2 files changed +27
-27
lines changed
2 files changed +27
-27
lines changed Original file line number Diff line number Diff line change @@ -2,16 +2,36 @@ import React from 'react';
2
2
import { StyleSheet , Text , View , WebView } from 'react-native' ;
3
3
import { registerForPushNotificationsAsync } from './lib/pushNotifications' ;
4
4
5
+ const loggedInUrlRegex = / m e m b e r s / ;
6
+ const mainUrl = 'https://www.timeoverflow.org/' ;
7
+
5
8
export default class App extends React . Component {
6
- componentDidMount ( ) {
7
- registerForPushNotificationsAsync ( ) ;
9
+ componentDidMount ( ) { }
10
+
11
+ registerForPushNotifications ( ) {
12
+ const token = await registerForPushNotificationsAsync ( ) ;
13
+
14
+ this . runRemoteTokenStoring ( token ) ;
15
+ }
16
+
17
+ onNavigationStateChange ( { url } ) {
18
+ if ( loggedInUrlRegex . test ( url ) ) {
19
+ this . registerForPushNotifications ( )
20
+ }
21
+ }
22
+
23
+ runRemoteTokenStoring ( token ) {
24
+ this . webview . injectJavaScript ( `window.TimeOverflowRegisterExpoDeviceToken(\'${ token } \');` ) ;
8
25
}
9
26
10
27
render ( ) {
11
28
return (
12
29
< WebView
13
- source = { { uri : 'https://www.timeoverflow.org/' } }
30
+ ref = { ref => ( this . webview = ref ) }
31
+ source = { { uri : mainUrl } }
14
32
style = { { marginTop : 20 } }
33
+ onNavigationStateChange = { this . onNavigationStateChange . bind ( this ) }
34
+ scalesPageToFit = { false }
15
35
/>
16
36
) ;
17
37
}
Original file line number Diff line number Diff line change @@ -3,9 +3,8 @@ import { Permissions, Notifications } from 'expo';
3
3
const PUSH_ENDPOINT = 'https://your-server.com/users/push-token' ;
4
4
5
5
export async function registerForPushNotificationsAsync ( ) {
6
- const { status : existingStatus } = await Permissions . getAsync (
7
- Permissions . NOTIFICATIONS
8
- ) ;
6
+ const { status : existingStatus } = await Permissions . getAsync ( Permissions . NOTIFICATIONS ) ;
7
+
9
8
let finalStatus = existingStatus ;
10
9
11
10
// only ask if permissions have not already been determined, because
@@ -18,29 +17,10 @@ export async function registerForPushNotificationsAsync() {
18
17
}
19
18
20
19
// Stop here if the user did not grant permissions
21
- if ( finalStatus !== 'granted' ) {
22
- return ;
23
- }
20
+ if ( finalStatus !== 'granted' ) return ;
24
21
25
22
// Get the token that uniquely identifies this device
26
23
let token = await Notifications . getExpoPushTokenAsync ( ) ;
27
24
28
- console . log ( 'user token' , token ) ;
29
-
30
- // POST the token to your backend server from where you can retrieve it to send push notifications.
31
- // return fetch(PUSH_ENDPOINT, {
32
- // method: 'POST',
33
- // headers: {
34
- // Accept: 'application/json',
35
- // 'Content-Type': 'application/json',
36
- // },
37
- // body: JSON.stringify({
38
- // token: {
39
- // value: token,
40
- // },
41
- // user: {
42
- // username: 'Brent',
43
- // },
44
- // }),
45
- // });
25
+ return token ;
46
26
}
You can’t perform that action at this time.
0 commit comments