Skip to content

Prove of concept to store the push notifications token #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,36 @@ import React from 'react';
import { StyleSheet, Text, View, WebView } from 'react-native';
import { registerForPushNotificationsAsync } from './lib/pushNotifications';

const loggedInUrlRegex = /members/;
const mainUrl = 'https://www.timeoverflow.org/';

export default class App extends React.Component {
componentDidMount() {
registerForPushNotificationsAsync();
componentDidMount() {}

registerForPushNotifications() {
const token = await registerForPushNotificationsAsync();

this.runRemoteTokenStoring(token);
}

onNavigationStateChange({ url }) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do the following, to avoid binding to this on the render method. It's magic.

-  onNavigationStateChange({ url }) { 
+ onNavigationStateChange = ({url}) => {
+ ...
+ }

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wat?! can you explain that? I'm just a poor rubyist trying to understand this...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

haha, maybe the current syntax is more accessible then :P

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I shouldn't be the one taken as example :trollface:

if (loggedInUrlRegex.test(url)) {
this.registerForPushNotifications()
}
}

runRemoteTokenStoring(token) {
this.webview.injectJavaScript(`window.TimeOverflowRegisterExpoDeviceToken(\'${token}\');`);
}

render() {
return (
<WebView
source={{uri: 'https://www.timeoverflow.org/'}}
ref={ref => (this.webview = ref)}
source={{ uri: mainUrl }}
style={{marginTop: 20}}
onNavigationStateChange={this.onNavigationStateChange.bind(this)}
scalesPageToFit={false}
/>
);
}
Expand Down
28 changes: 4 additions & 24 deletions lib/pushNotifications.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import { Permissions, Notifications } from 'expo';
const PUSH_ENDPOINT = 'https://your-server.com/users/push-token';

export async function registerForPushNotificationsAsync() {
const { status: existingStatus } = await Permissions.getAsync(
Permissions.NOTIFICATIONS
);
const { status: existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS);

let finalStatus = existingStatus;

// only ask if permissions have not already been determined, because
Expand All @@ -18,29 +17,10 @@ export async function registerForPushNotificationsAsync() {
}

// Stop here if the user did not grant permissions
if (finalStatus !== 'granted') {
return;
}
if (finalStatus !== 'granted') return;

// Get the token that uniquely identifies this device
let token = await Notifications.getExpoPushTokenAsync();

console.log('user token', token);

// POST the token to your backend server from where you can retrieve it to send push notifications.
// return fetch(PUSH_ENDPOINT, {
// method: 'POST',
// headers: {
// Accept: 'application/json',
// 'Content-Type': 'application/json',
// },
// body: JSON.stringify({
// token: {
// value: token,
// },
// user: {
// username: 'Brent',
// },
// }),
// });
return token;
}