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.
EDIT
I have updated the code. Now I am able to get firebase.messaging().onMessage()
code working and receive a trigger in the foreground. Still unable to get the notification when the app is in the background. Below is the change which I have made.
const fcmToken = await firebase.messaging().getToken();firebase.messaging().ios.registerForRemoteNotifications().then((flag)=>{ console.log("registered", flag); }).catch((err)=>{ console.log("message", err); });
AppDelegate.m
#import "AppDelegate.h"#import <React/RCTBridge.h>#import <React/RCTBundleURLProvider.h>#import <React/RCTRootView.h>#import <Firebase.h>#import "RNFirebaseNotifications.h"#import "RNFirebaseMessaging.h"@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ [FIRApp configure]; [[UNUserNotificationCenter currentNotificationCenter] setDelegate:self]; [RNFirebaseNotifications configure]; //[FIRApp configure]; [Fabric with:@[[Crashlytics class]]]; RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions]; RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge moduleName:@"CymmAuth" initialProperties:nil]; rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1]; self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds]; UIViewController *rootViewController = [UIViewController new]; rootViewController.view = rootView; self.window.rootViewController = rootViewController; [self.window makeKeyAndVisible]; return YES;}- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge{#if DEBUG return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];#else return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];#endif}- (void)application:(UIApplication *)application didReceiveRemoteNotification:(nonnull NSDictionary *)userInfofetchCompletionHandler:(nonnull void (^)(UIBackgroundFetchResult))completionHandler{ [[RNFirebaseNotifications instance] didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];}- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings { [[RNFirebaseMessaging instance] didRegisterUserNotificationSettings:notificationSettings];}-(void) userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler { [[RNFirebaseMessaging instance] didReceiveRemoteNotification:response.notification.request.content.userInfo]; completionHandler();}@end
Do let me know if I am missing anything. firebase.notifications().onNotification()
doesn't get triggered