ios - @react-native-firebase/messaging : TypeError: (0 , _messaging.default)(...).registerForRemoteNotifications 不是一个函数

标签 ios react-native push-notification react-native-firebase

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"


我最近将我的 react-native 版本从 0.63.0 降级到 0.61.0。在android中一切正常,但在ios模拟器&&真实设备中messaging().registerForRemoteNotifications()抛出错误。

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 信息

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


我的 App.js文件
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)
      }
    }
我的 AppDelegate.m文件
/**
 * 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>

最佳答案

我通过拨打 PushNotification.configure() 解决了这个问题在 index.js而不是间接地在组件中。对我来说,这是一个与 react-native-push-notification 相关的问题并且在调用 .configure 时发生在我的 Android 模拟器中在不正确的地方。 “如果你[在那里拨打 .configure],通知处理程序将不会触发”。需要根据documentation在加载任何组件之前调用.
请注意,iOS 模拟器只是缺少一些功能,因此请根据 Platform.OS 进行处理。的 react-native使用您认为合适的模拟或替代方案。

关于ios - @react-native-firebase/messaging : TypeError: (0 , _messaging.default)(...).registerForRemoteNotifications 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63772643/

相关文章:

ios - 枚举类型的开关大小写

ios - 来自自定义 tableview 单元格的 uilabel 未显示在 tableviewcontroller 中

node.js - 世博会启动后崩溃

reactjs - react native 文本输入;按下 "Done"时触发事件

ios - iOS 解锁时显示不同的通知文本

ios - 使用数组的 TableView 不起作用-xcode 7.3

ios - 如何在基于导航的应用程序中更改 Push 和 Pop 动画

android - Android 模拟器上的 Expo 无法运行,出现问题

ios - 如何在 iOS 中获取 One Signal 用户的唯一玩家 ID?

android - 通知自动取消不适用于 Android Lollipop