I have a react native project. I am able to receive the FCM token successfully but when trying to send a notification, the app doesn't receive the notification.
The steps I followed are as below:
- Created a project in Firebase Console.
- Added the Firebase .plist in the projectName through Xcode.
- ran
npm install --save react-native-firebase
- Added in podfile:
pod ‘Firebase/Core’
- ran
pod install
- Update AppDelegate.m with
#import <Firebase.h>
and[FIRApp configure];
- Added the APNS in the Firebase Dashboard for iOS App Cloud Messaging.
- Updated the capabilities with Push Notification and Background Modes > Remote notification
- In info.plist FIRAnalyticsDebugEnabled, FirebaseAppDelegateProxyEnabled, FirebaseScreenReportingEnabled is set to No
using const fcmToken = await firebase.messaging().getToken();
I am able to get token.
Below is the code for the notification listener.
async createNotificationListeners() { /* * Triggered when a particular notification has been received in foreground * */ this.notificationListener = firebase.notifications().onNotification((notification) => { const { title, body } = notification; this.custom_data = notification.data; const localNotification = new firebase.notifications.Notification({ show_in_foreground: true, }) .setSound('default') .setNotificationId(notification.notificationId) .setTitle(notification.title) .setBody(notification.body) firebase.notifications() .displayNotification(localNotification) .catch(err => Alert.alert(err)); }); /* * If your app is in foreground and background, you can listen for when a notification is clicked / tapped / opened as follows: * */ this.notificationOpenedListener = firebase.notifications().onNotificationOpened((notificationOpen) => { if ("title" in notificationOpen.notification.data) { const { title, body, secret_key, user_id, realm_id, user_os, user_location } = notificationOpen.notification.data; this.props.navigation.navigate('Verify', { title: title, body: body, secret_key: secret_key, user_id: user_id, realm_id: realm_id, user_os: user_os, user_location: user_location }); } else { const { title, body, secret_key, user_id, realm_id, user_os, user_location } = this.custom_data; this.props.navigation.navigate('Verify', { title: title, body: body, secret_key: secret_key, user_id: user_id, realm_id: realm_id, user_os: user_os, user_location: user_location }); } }); /* * 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, secret_key, user_id, realm_id, user_os, user_location } = notificationOpen.notification.data; this.props.navigation.navigate('FCM', { title: title, body: body, secret_key: secret_key, user_id: user_id, realm_id: realm_id, user_os: user_os, user_location: user_location }); } /* * Triggered for data only payload in foreground * */ this.messageListener = firebase.messaging().onMessage((message) => { console.log("JSON.stringify:", JSON.stringify(message)); });}
Please do let me know if more details required.