Skip to content

Commit f130804

Browse files
authored
Merge pull request #1 from coopdevs/feature/push-token-storing
Prove of concept to store the push notifications token
2 parents 6948aae + 03e3a7c commit f130804

File tree

2 files changed

+27
-27
lines changed

2 files changed

+27
-27
lines changed

App.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,36 @@ import React from 'react';
22
import { StyleSheet, Text, View, WebView } from 'react-native';
33
import { registerForPushNotificationsAsync } from './lib/pushNotifications';
44

5+
const loggedInUrlRegex = /members/;
6+
const mainUrl = 'https://www.timeoverflow.org/';
7+
58
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}\');`);
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
}

lib/pushNotifications.js

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ import { Permissions, Notifications } from 'expo';
33
const PUSH_ENDPOINT = 'https://your-server.com/users/push-token';
44

55
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+
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
}

0 commit comments

Comments
 (0)