Closed marc0s closed 5 years ago
I was facing the same issue. Here is how I fixed it (iOS specific, android was working out of the box)-
async componentDidMount() {
this.checkPermission();
this.createNotificationListeners();
}
4.
async checkPermission() {
const enabled = await firebase.messaging().hasPermission();
if (enabled) {
this.getToken();
} else {
this.requestPermission();
}
}
async requestPermission() {
try {
await firebase.messaging().requestPermission();
// User has authorised
this.getToken();
} catch (error) {
// User has rejected permissions
console.log('permission rejected');
}
}
async getToken() {
try {
const enabled = await firebase.messaging().hasPermission();
if (!enabled) {
await firebase.messaging().requestPermission();
}
const fcmToken = await firebase.messaging().getToken();
if (fcmToken) {
console.log("got token");
console.log('fcm token:', fcmToken); //-->use this token from the console to send a post request via postman
this.setState({ fcmToken });
return fcmToken;
}
} catch (error) {
console.warn('notification token error', error);
}
}
5.
async createNotificationListeners() {
/*
* Triggered when a particular notification has been received in foreground
* */
this.notificationListener = firebase.notifications().onNotification((notification) => {
const { title, body } = notification;
this.showAlert(title, body);
});
/*
* If your app is in background, you can listen for when a notification is clicked / tapped / opened as follows:
* */
this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => {
const { title, body } = notificationOpen.notification;
this.showAlert(title, body);
});
/*
* If your app is closed, you can check if it was opened by a notification being clicked / tapped / opened as follows:
* */
const notificationOpen = await firebase.notifications().getInitialNotification();
if (notificationOpen) {
const { title, body } = notificationOpen.notification;
this.showAlert(title, body);
}
/*
* Triggered for data only payload in foreground
* */
this.messageListener = firebase.messaging().onMessage((message) => {
//process data message
console.log(JSON.stringify(message));
});
}
For a more detailed setup/citation- https://medium.com/@anum.amin/react-native-integrating-push-notifications-using-fcm-349fff071591
Hi, as stated in https://firebase.google.com/docs/cloud-messaging/http-server-ref#table9 whenever a
NotRegistered
error from FCM is received, app server should drop the failing token and never use it again to send push notifications.I have added such a feature by firing
unregister-push-token
inside thefcm_handler
if such an error is received. See diff below.The problem with this patch is that
mod_push_appserver
keeps the information about whether the registration works ok or not by settinglast_successful_push
orlast_push_error
accordingly and this undoes the deleting of the registration just done infcm_handler
.I'm not sure how to tackle this. Should the return value of the
fcm_handler
provide more information on what to do? Or maybe just because an error happened, this token should not be used anymore even if it's present in the storage?Thanks!