File tree Expand file tree Collapse file tree 2 files changed +27
-27
lines changed Expand file tree Collapse file tree 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';
22import { StyleSheet , Text , View , WebView } from 'react-native' ;
33import { registerForPushNotificationsAsync } from './lib/pushNotifications' ;
44
5+ const loggedInUrlRegex = / m e m b e r s / ;
6+ const mainUrl = 'https://www.timeoverflow.org/' ;
7+
58export 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 } \');` ) ;
825 }
926
1027 render ( ) {
1128 return (
1229 < WebView
13- source = { { uri : 'https://www.timeoverflow.org/' } }
30+ ref = { ref => ( this . webview = ref ) }
31+ source = { { uri : mainUrl } }
1432 style = { { marginTop : 20 } }
33+ onNavigationStateChange = { this . onNavigationStateChange . bind ( this ) }
34+ scalesPageToFit = { false }
1535 />
1636 ) ;
1737 }
Original file line number Diff line number Diff line change @@ -3,9 +3,8 @@ import { Permissions, Notifications } from 'expo';
33const PUSH_ENDPOINT = 'https://your-server.com/users/push-token' ;
44
55export async function registerForPushNotificationsAsync ( ) {
6- const { status : existingStatus } = await Permissions . getAsync (
7- Permissions . NOTIFICATIONS
8- ) ;
6+ const { status : existingStatus } = await Permissions . getAsync ( Permissions . NOTIFICATIONS ) ;
7+
98 let finalStatus = existingStatus ;
109
1110 // only ask if permissions have not already been determined, because
@@ -18,29 +17,10 @@ export async function registerForPushNotificationsAsync() {
1817 }
1918
2019 // Stop here if the user did not grant permissions
21- if ( finalStatus !== 'granted' ) {
22- return ;
23- }
20+ if ( finalStatus !== 'granted' ) return ;
2421
2522 // Get the token that uniquely identifies this device
2623 let token = await Notifications . getExpoPushTokenAsync ( ) ;
2724
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 ;
4626}
You can’t perform that action at this time.
0 commit comments