My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Expo push notification

vivek sharma's photo
vivek sharma
·Apr 6, 2020

My previous expo Project it was in sdk 33 & i upgrade to 36. this is my code

import React from 'react'; import { Text, View, Button, Vibration, Platform, Alert } from 'react-native'; import { Notifications } from 'expo'; import * as Permissions from 'expo-permissions'; import Constants from 'expo-constants';

export default class AppContainer extends React.Component { state = { expoPushToken: '', notification: {}, };

registerForPushNotificationsAsync = async () => { if (Constants.isDevice) { const { status: existingStatus } = await Permissions.getAsync(Permissions.NOTIFICATIONS); console.log(existingStatus) let finalStatus = existingStatus; if (existingStatus !== 'granted') { const { status } = await Permissions.askAsync(Permissions.NOTIFICATIONS); finalStatus = status; } if (finalStatus !== 'granted') { Alert.alert('Failed to get push token for push notification!'); return; } console.log("Fetching token") let tokenssss = await Notifications.getExpoPushTokenAsync() console.log(tokenssss) Alert.alert(tokenssss); this.setState({ expoPushToken: tokenssss }); } else { alert('Must use physical device for Push Notifications'); }

if (Platform.OS === 'android') {
  Notifications.createChannelAndroidAsync('default', {
    name: 'default',
    sound: true,
    priority: 'max',
    vibrate: [0, 250, 250, 250],
  });
}

}; componentDidMount() { this.registerForPushNotificationsAsync(); this._notificationSubscription = Notifications.addListener(this._handleNotification); }

_handleNotification = notification => { Vibration.vibrate(); console.log(notification); this.setState({ notification: notification }); };

// Can use this function below, OR use Expo's Push Notification Tool-> expo.io/dashboard/notifications sendPushNotification = async () => { const message = { to: this.state.expoPushToken, sound: 'default', title: 'Original Title', body: 'And here is the body!', data: { data: 'goes here' }, _displayInForeground: true, }; const response = await fetch('exp.host/--/api/v2/push/send', { method: 'POST', headers: { Accept: 'application/json', 'Accept-encoding': 'gzip, deflate', 'Content-Type': 'application/json', }, body: JSON.stringify(message), }); };

render() { return ( <View style={{ flex: 1, alignItems: 'center', justifyContent: 'space-around', }}> <View style={{ alignItems: 'center', justifyContent: 'center' }}> <Text>Origin: {this.state.notification.origin}</Text> <Text>Data: {JSON.stringify(this.state.notification.data)}</Text> </View> <Button title={'Press to Send Notification'} onPress={() => this.sendPushNotification()} /> </View> ); } }

it can't generate a token it is a bug