diff --git a/data-sources/firebase-data.js b/data-sources/firebase-data.js index 60cd8b0..bccb18b 100644 --- a/data-sources/firebase-data.js +++ b/data-sources/firebase-data.js @@ -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} - 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() + }); +} + /** * * @param {string} uid - user id diff --git a/libs/pushTokens.js b/libs/pushTokens.js new file mode 100644 index 0000000..9e83843 --- /dev/null +++ b/libs/pushTokens.js @@ -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( + 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, { +// method: 'POST', +// headers: { +// Accept: 'application/json', +// 'Content-Type': 'application/json', +// }, +// body: JSON.stringify({ +// token: { +// value: token, +// }, +// user: { +// username: 'Brent', +// }, +// }), +// }); +} \ No newline at end of file diff --git a/screens/dashboard/index.js b/screens/dashboard/index.js index 036cfbb..f34ed07 100644 --- a/screens/dashboard/index.js +++ b/screens/dashboard/index.js @@ -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'; @@ -84,6 +85,10 @@ class Dashboard extends Component { } } + pushTokenAlert() { + getPushToken() + } + ellipsisLogoutAlert() { const logoutCallback = this.props.actions.logout; @@ -162,7 +167,7 @@ class Dashboard extends Component { }} > this.ellipsisLogoutAlert()} + onPress={() => this.pushTokenAlert()} underlayColor='transparent' style={{ width: 300,