Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@react-native-firebase/messaging : TypeError: (0 , _messaging.default)(...).registerForRemoteNotifications is not a function

I am using @react-native-firebase/app": "^8.2.0", @react-native-firebase/messaging and react-native v0.61.0. also using @react-native-community/push-notification-ios": "^1.4.0" and "react-native-push-notification": "^4.0.0"

I have recently downgraded my react-native version from 0.63.0 to 0.61.0. In android everything works perfect but in ios simulator && real device messaging().registerForRemoteNotifications() throw error.

TypeError: (0 , _messaging.default)(...).registerForRemoteNotifications is not a function TypeError: (0 , _messaging.default)(...).registerForRemoteNotifications is not a function at App.componentDidMount

enter image description here

React-Native Info

System: OS: macOS 10.15.5 CPU: (4) x64 Intel(R) Core(TM) i5-3470S CPU @ 2.90GHz Memory: 45.55 MB / 16.00 GB Shell: 3.2.57 - /bin/bash Binaries: Node: 12.15.0 - /usr/local/bin/node npm: 6.13.4 - /usr/local/bin/npm Watchman: 4.9.0 - /usr/local/bin/watchman SDKs: iOS SDK: Platforms: iOS 13.5, DriverKit 19.0, macOS 10.15, tvOS 13.4, watchOS 6.2 IDEs: Xcode: 11.5/11E608c - /usr/bin/xcodebuild npmPackages: react: 16.9.0 => 16.9.0 react-native: 0.61.0 => 0.61.0 npmGlobalPackages: react-native-cli: 2.0.1

My App.js file

const hasPermissions = await messaging().hasPermission()
    if (hasPermissions) {
      await messaging().registerForRemoteNotifications()
      await new Promise((resolve, reject) => setTimeout(() => resolve(), 1000))

      token = await messaging().getToken()
      let fctk = `getting fcm token ${token}`
      Alert.alert(fctk)
      console.log('FCM token', token)
    } else {
      const { status } = await requestNotifications(['alert', 'sound'])

      if (status === 'granted') {
        await messaging().registerForRemoteNotifications()
        await new Promise((resolve, reject) => setTimeout(() => resolve(), 1000))

        token = await messaging().getToken()
        let fctk = `getting fcm token ${token}`
        Alert.alert(fctk)
        console.log('FCM token has been received', token)
      }
    }

my AppDelegate.m file

/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 */
#import <Firebase.h>
#import "AppDelegate.h"

#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <UserNotifications/UserNotifications.h>
#import <RNCPushNotificationIOS.h>
#import <GoogleMaps/GoogleMaps.h>
#import "RNSplashScreen.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
  if ([FIRApp defaultApp] == nil) {
    [FIRApp configure];
  }

   [GMSServices provideAPIKey:@"AIzaSyBALbK0zTosrX4J1sl9-k1wJt14Zuwk37M"];

  RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
  RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
                                                   moduleName:@"APPBusinessPlaza63"
                                            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];
  
  // Define UNUserNotificationCenter
   UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
   center.delegate = self;
  [RNSplashScreen show];
  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 to register for notifications
- (void)application:(UIApplication *)application didRegisterUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
{
 [RNCPushNotificationIOS didRegisterUserNotificationSettings:notificationSettings];
}
// 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 *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
  [RNCPushNotificationIOS didReceiveRemoteNotification:userInfo fetchCompletionHandler:completionHandler];
}
// Required for the registrationError event.
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
 [RNCPushNotificationIOS didFailToRegisterForRemoteNotificationsWithError:error];
}
// IOS 10+ Required for localNotification event
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
         withCompletionHandler:(void (^)(void))completionHandler
{
  [RNCPushNotificationIOS didReceiveNotificationResponse:response];
  completionHandler();
}
// IOS 4-10 Required for the localNotification event.
- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
 [RNCPushNotificationIOS didReceiveLocalNotification:notification];
}

//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
  completionHandler(UNAuthorizationOptionSound | UNAuthorizationOptionAlert | UNAuthorizationOptionBadge);
}

@end
Info.plist file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>en</string>
    <key>CFBundleDisplayName</key>
    <string>$(PRODUCT_NAME)</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>$(PRODUCT_NAME)</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLName</key>
            <string>Bundle ID</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>com.webmascot.bp</string>
            </array>
        </dict>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>com.googleusercontent.apps.558394662083-po8okplh2v0cvc91r76rkau73tddkpj7</string>
            </array>
        </dict>
        <dict>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>fb469065720287765</string>
            </array>
        </dict>
    </array>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>localhost</key>
            <dict>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
            </dict>
        </dict>
    </dict>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string></string>
    <key>UIAppFonts</key>
    <array>
        <string>Axiforma_Bold.ttf</string>
        <string>Axiforma_Book.ttf</string>
        <string>Axiforma_Light.ttf</string>
        <string>Axiforma_Medium.ttf</string>
        <string>Axiforma_Regular.ttf</string>
        <string>MaterialIcons.ttf</string>
        <string>MaterialCommunityIcons.ttf</string>
        <string>Poppins-Black.ttf</string>
        <string>Poppins-BlackItalic.ttf</string>
        <string>Poppins-Bold.ttf</string>
        <string>Poppins-BoldItalic.ttf</string>
        <string>Poppins-ExtraBold.ttf</string>
        <string>Poppins-ExtraBoldItalic.ttf</string>
        <string>Poppins-ExtraLight.ttf</string>
        <string>Poppins-ExtraLightItalic.ttf</string>
        <string>Poppins-Italic.ttf</string>
        <string>Poppins-Light.ttf</string>
        <string>Poppins-LightItalic.ttf</string>
        <string>Poppins-Medium.ttf</string>
        <string>Poppins-MediumItalic.ttf</string>
        <string>Poppins-Regular.ttf</string>
        <string>Poppins-SemiBold.ttf</string>
        <string>Poppins-SemiBoldItalic.ttf</string>
        <string>Poppins-Thin.ttf</string>
        <string>Poppins-ThinItalic.ttf</string>
        <string>Roboto-Black.ttf</string>
        <string>Roboto-BlackItalic.ttf</string>
        <string>Roboto-Bold.ttf</string>
        <string>Roboto-BoldItalic.ttf</string>
        <string>Roboto-Italic.ttf</string>
        <string>Roboto-Light.ttf</string>
        <string>Roboto-Medium.ttf</string>
        <string>Roboto-Regular.ttf</string>
        <string>Roboto-Thin.ttf</string>
        <string>Roboto_medium.ttf</string>
        <string>Roboto.ttf</string>
        <string>rubicon-icon-font.ttf</string>
    </array>
    <key>UIBackgroundModes</key>
    <array>
        <string>fetch</string>
        <string>remote-notification</string>
    </array>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIRequiredDeviceCapabilities</key>
    <array>
        <string>armv7</string>
    </array>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
</dict>
</plist>
like image 491
Menon Hasan Avatar asked Nov 15 '22 05:11

Menon Hasan


1 Answers

I fixed this problem by calling PushNotification.configure() in index.js rather than indirectly in a component. This, for me, is a problem related to react-native-push-notification and happens in my Android simulators when calling .configure in the incorrect place. "If you do [call .configure there], notification handlers will not fire". It is is necessary to call before any components are loaded according to documentation.

Be aware that iOS simulators simply lack some functionality so handle based on Platform.OS of react-native with mocks or alternatives as you find appropriate.

like image 121
haleonj Avatar answered Jan 19 '23 06:01

haleonj