I have update my react native firebase to v6 and used this package to show notification on iOS. But I am not receiving notification in any of the state: foreground, background and when app is in closed state.
This is my AppDelegate.h file::
#import <React/RCTBridgeDelegate.h>#import <UIKit/UIKit.h>#import <UserNotifications/UNUserNotificationCenter.h>@interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate, UNUserNotificationCenterDelegate>@property (nonatomic, strong) UIWindow *window;@end
AppDelegate.m file::
#import "AppDelegate.h"#import <React/RCTBridge.h>#import <React/RCTBundleURLProvider.h>#import <React/RCTRootView.h>#import <ReactNativeNavigation/ReactNativeNavigation.h>#import "RNSplashScreen.h"#import <Firebase.h>#import "SDImageCodersManager.h"#import <SDWebImageWebPCoder/SDImageWebPCoder.h>#import <UserNotifications/UserNotifications.h>#import <RNCPushNotificationIOS.h>@implementation AppDelegate- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ NSURL *jsCodeLocation = [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil]; [ReactNativeNavigation bootstrap:jsCodeLocation launchOptions:launchOptions];// if ([FIRApp defaultApp] == nil) { [FIRApp configure];// } // show splash screen [RNSplashScreen show]; // Register support for WebP images. [SDImageCodersManager.sharedManager addCoder:SDImageWebPCoder.sharedCoder]; // Define UNUserNotificationCenter UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; center.delegate = self; return YES;}- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge{#if DEBUG return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];#else return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];#endif}// Required for the register event.- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{ [RNCPushNotificationIOS didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];}// Required for the notification event. You must call the completion handler after handling the remote notification.- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfofetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler{ [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];}// Required for the registrationError event.- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error{ [RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];}// Required for localNotification event- (void)userNotificationCenter:(UNUserNotificationCenter *)centerdidReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler{ [RNCPushNotificationIOS didReceiveNotificationResponse:response];}//Called when a notification is delivered to a foreground app.-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{ completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);}@end
After this inside my App.js file I called this function inside constructor::
NotificationService.getInstance().initializeNotificationServiceForIOS();
I have created one service file where I have written the notification logic for android and iOS. Push notifications working fine on android but not getting in case of iOS. This is a code inside my service file for iOS.
import { Alert } from "react-native"; import PushNotification from 'react-native-push-notification'; import messaging from '@react-native-firebase/messaging'; import NotificationHandler from './NotificationHandler'; import PushNotificationIOS from '@react-native-community/push-notification-ios'; import AsyncStorage from '@react-native-community/async-storage'; //constants import { androidNotificationChannelId, isIOS } from '../../constants'; import LocalStorageKeys from '../../constants/LocalStorageKeys'; export default class NotificationService { /** * @returns {NotificationService} */ static getInstance() { if (NotificationService.myInstance == null) { NotificationService.myInstance = new NotificationService(); } return this.myInstance; } initializeNotificationServiceForIOS() { PushNotificationIOS.addEventListener('register', this.onRegistered); PushNotificationIOS.addEventListener('registrationError', this.onRegistrationError); PushNotificationIOS.addEventListener('notification', this.onRemoteNotification); PushNotificationIOS.addEventListener('localNotification', this.onLocalNotification); PushNotificationIOS.requestPermissions().then( (data) => { console.log('PushNotificationIOS.requestPermissions', data); }, (data) => { console.log('PushNotificationIOS.requestPermissions failed', data); }, ); } onRegistered = (deviceToken) => { console.log("ios deviceToken", deviceToken) AsyncStorage.setItem(LocalStorageKeys.DEVICE_TOKEN, deviceToken); }; onRegistrationError = (error) => { Alert.alert('Failed To Register For Remote Push', `Error (${error.code}): ${error.message}`, [ { text: 'Dismiss', onPress: null, }, ], ); }; onRemoteNotification = (notification) => { const isClicked = notification.getData().userInteraction === 1; const result = ` Title: ${notification.getTitle()};\n Subtitle: ${notification.getSubtitle()};\n Message: ${notification.getMessage()};\n badge: ${notification.getBadgeCount()};\n sound: ${notification.getSound()};\n category: ${notification.getCategory()};\n content-available: ${notification.getContentAvailable()};\n Notification is clicked: ${String(isClicked)}.`; if (notification.getTitle() == undefined) { Alert.alert('Silent push notification Received', result, [ { text: 'Send local push', onPress: sendLocalNotification, }, ]); } else { Alert.alert('Push Notification Received', result, [ { text: 'Dismiss', onPress: null, }, ]); } }; onLocalNotification = (notification) => { const isClicked = notification.getData().userInteraction === 1; Alert.alert('Local Notification Received', `Alert title: ${notification.getTitle()}, Alert subtitle: ${notification.getSubtitle()}, Alert message: ${notification.getMessage()}, Badge: ${notification.getBadgeCount()}, Sound: ${notification.getSound()}, Thread Id: ${notification.getThreadID()}, Action Id: ${notification.getActionIdentifier()}, User Text: ${notification.getUserText()}, Notification is clicked: ${String(isClicked)}.`, [ { text: 'Dismiss', onPress: null, }, ], ); }; }
I have added console inside the "onRemoteNotification" and "onLocalNotification" but nothing happened after sending notification. Also I rechecked the apns certificate and other setting on firebase, this is completed still i am not getting any test sms from firebase and also on any event while sending notification remotely.
Please help and suggest something because I stuck here.