Skip to content
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

Generate push token #170

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
15 changes: 15 additions & 0 deletions data-sources/firebase-data.js
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,21 @@ export function updateProfile(user: User): Promise {
});
}

/**
* Saves the push notification token to firestore. The token is needed to
* send push notifications to users.
* @param {string} token - the push token generated by the user's phone
* @param {User} user - the current user object
* @returns {Promise<any>} - Promise
*/
export function setUserPushToken(token: string, user: User) {
const data = deconstruct(user);
return db.collection('users').doc(user.uid).update({
pushToken: token,
updated: firebase.firestore.FieldValue.serverTimestamp()
});
}
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't think this branch uses this function yet.


/**
*
* @param {string} uid - user id
Expand Down
50 changes: 50 additions & 0 deletions libs/pushTokens.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { Notifications } from 'expo';
import * as Permissions from 'expo-permissions';
import {Alert} from 'react-native';

const PUSH_ENDPOINT = 'https://your-server.com/users/push-token';

export default async function registerForPushNotificationsAsync() {
const { status: existingStatus } = await Permissions.getAsync(
Copy link
Member Author

Choose a reason for hiding this comment

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

I don't know if there's a better place to keep P.N. related stuff. Using the libs dir might make sense if there isn't a more intuitive place.

Permissions.NOTIFICATIONS
);
let finalStatus = existingStatus;

// only ask if permissions have not already been determined, because
// iOS won't necessarily prompt the user a second time.
if (existingStatus !== 'granted') {
// Android remote notification permissions are granted during the app
// install, so this will only ask on iOS
const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS);
finalStatus = status;
}

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

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

// lets see if we can display it as an alert...
Alert.alert('Push Token', token);


// POST the token to your backend server from where you can retrieve it to send push notifications.
// return fetch(PUSH_ENDPOINT, {
Copy link
Member Author

Choose a reason for hiding this comment

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

Obviously we would want to send the token to the server using redux and the appropriate actions.

// method: 'POST',
// headers: {
// Accept: 'application/json',
// 'Content-Type': 'application/json',
// },
// body: JSON.stringify({
// token: {
// value: token,
// },
// user: {
// username: 'Brent',
// },
// }),
// });
}
7 changes: 6 additions & 1 deletion screens/dashboard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import commonStyles from '../../styles/common';
import MoneyMeter from '../../components/money-meter';
import MenuCircle from '../../components/menu-circle';
import GoalsBox from '../../components/goals-box';
import getPushToken from '../../libs/pushTokens'

import pkg from '../../package.json';

Expand Down Expand Up @@ -84,6 +85,10 @@ class Dashboard extends Component<PropsType> {
}
}

pushTokenAlert() {
getPushToken()
}

ellipsisLogoutAlert() {
const logoutCallback = this.props.actions.logout;

Expand Down Expand Up @@ -162,7 +167,7 @@ class Dashboard extends Component<PropsType> {
}}
>
<TouchableHighlight
onPress={() => this.ellipsisLogoutAlert()}
onPress={() => this.pushTokenAlert()}
Copy link
Member Author

Choose a reason for hiding this comment

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

This is how I triggered the push token generation.

underlayColor='transparent'
style={{
width: 300,
Expand Down