Hello everyone.
I am using expo manage project . The code below work on my device through Snack but it’s not working in my project when I tried implementing it. Is there something wrong.
import React from ‘react’;
import { Notifications, Permissions, Constants } from ‘expo’;
import { Text, View, Button } from ‘react-native’;
const YOUR_PUSH_TOKEN = ‘’;
export default class AppContainer extends React.Component {
state = {
notification: {},
};
registerForPushNotificationsAsync = async () => {
if (Constants.isDevice) {
const { status: existingStatus } = await Permissions.getAsync(
Permissions.NOTIFICATIONS
);
let finalStatus = existingStatus;
if (existingStatus !== ‘granted’) {
const { status } = await Permissions.askAsync(
Permissions.NOTIFICATIONS
);
finalStatus = status;
}
if (finalStatus !== ‘granted’) {
alert(‘Failed to get push token for push notification!’);
return;
}
let token = await Notifications.getExpoPushTokenAsync();
console.log(token);
} else {
alert(‘Must use physical device for Push Notifications’);
}
};
componentDidMount() {
this.registerForPushNotificationsAsync();
// Handle notifications that are received or selected while the app
// is open. If the app was closed and then opened by tapping the
// notification (rather than just tapping the app icon to open it),
// this function will fire on the next tick after the app starts
// with the notification data.
this._notificationSubscription = Notifications.addListener(
this._handleNotification
);
}
_handleNotification = notification => {
this.setState({ notification: notification });
console.log(notification);
// Notifications.presentLocalNotificationAsync({
// “to”: ‘ExponentPushToken[cIV_jcKcFwN3bQwORLSsyW]’,
// “sound”: “default”,
// “title”:“Original Title”,
// “body”: “And here is the body!”,
// “data”: {“data”:“goes here”}
// });
};
// Can use this function below, OR use Expo’s Push Notification Tool-> Push Notification Tool — Expo
sendPushNotification = async () => {
const message = {
to: ‘ExponentPushToken[cIV_jcKcFwN3bQwORLSsyW]’,
sound: ‘default’,
title: ‘Original Title’,
body: ‘And here is the body!’,
data: { data: ‘goes here’ },
};
const response = await fetch(‘https://exp.host/--/api/v2/push/send’, {
method: ‘POST’,
headers: {
Accept: ‘application/json’,
‘Accept-encoding’: ‘gzip, deflate’,
‘Content-Type’: ‘application/json’,
},
body: JSON.stringify(message),
});
const data = response._bodyInit;
console.log(Status & Response ID-> ${data}
);
};
render() {
return (
<View
style={{
flex: 1,
alignItems: ‘center’,
justifyContent: ‘space-around’,
}}>
<View style={{ alignItems: ‘center’, justifyContent: ‘center’ }}>
Origin: {this.state.notification.origin}
Data: {JSON.stringify(this.state.notification.data)}
<Button
title={‘Press to Send Notification’}
onPress={() => this.sendPushNotification()}
/>
);
}
}
/* TO GET PUSH RECEIPTS, RUN THE FOLLOWING COMMAND IN TERMINAL, WITH THE RECEIPTID SHOWN IN THE CONSOLE LOGS
curl -H "Content-Type: application/json" -X POST "https://exp.host/--/api/v2/push/getReceipts" -d '{
"ids": ["YOUR RECEIPTID STRING HERE"]
}'
*/