Push Notifications

Class 15 - Push Notifications

Expo Push Notifications

Allow Notification actions.js

import { Notifications } from 'expo';

Permissions.getAsync(Permissions.NOTIFICATIONS).then(function(result){
  if (result.status === 'granted') {
    Notifications.getExpoPushTokenAsync().then(function(token){
      firebase.database().ref('cards/' + firebase.auth().currentUser.uid ).update({ token: token });
      dispatch({ type: 'NOTIFICATION_TOKEN', payload: token });
    })
  }
})

Send Notification actions.js

export function sendNotification(id, name, text){
  return function(dispatch){
    firebase.database().ref('cards/' + id).once('value', (snap) => {
      if(snap.val().token != null){

        return fetch('https://exp.host/--/api/v2/push/send', {
          method: 'POST',
          headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json',
          },
          body: JSON.stringify({
            to: snap.val().token,
            title: name,
            body: text,
            badge: 1,
          }),
        });

      }
    });
  }
}

Chat.js

this.props.dispatch(
  sendNotification(
    this.props.navigation.state.params.user.id, 
    messages[0].user.name, 
    messages[0].text
  )
)

Written by Tim Moreton who lives and works
in Philadelphia building useful things.