首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >@ function本机-火基/消息传递: TypeError:(0,TypeError不是一个函数

@ function本机-火基/消息传递: TypeError:(0,TypeError不是一个函数
EN

Stack Overflow用户
提问于 2020-09-07 06:51:47
回答 1查看 879关注 0票数 6

我使用@react-本机-firebase/app:"^8.2.0",@react-本机-firebase/消息传递和react本机版本0.61.0。也使用@react-native-community/push-notification-ios":"^1.4.0“和”react-本机-推-通知“:"^4.0.0”。

我最近已经将我的react原生版本从0.63.0降级到0.61.0。在安卓系统中,一切都很完美,但在ios模拟器&真实设备中,messaging().registerForRemoteNotifications()抛出了错误。

_messaging.default)(...).registerForRemoteNotifications不是一个函数TypeError:(0,_messaging.default)(...).registerForRemoteNotifications不是App.componentDidMount中的一个函数

React本机信息

操作系统: macOS 10.15.5 CPU:(4) x64 Intel(R) Core(商标) i5-3470S CPU @ 2.90GHz内存:45.55MB/16.00GB Shell: 3.2.57 - /bin/bash二进制文件:节点: 12.15.0 - /usr/local/bin/ Node : 6.13.4 - /usr/local/bin/npm守望者: 4.9.0 -/usr/local/bin/x64 SDK: iOS SDK:平台: 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原生: 0.61.0 => 0.61.0 npmGlobalPackages: cli本机-cli: 2.0.1

我的App.js文件

代码语言:javascript
复制
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文件

代码语言:javascript
复制
/**
 * 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
代码语言:javascript
复制
Info.plist file
代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

发布于 2021-06-23 15:18:16

我通过在PushNotification.configure()中而不是在组件中间接调用index.js来解决这个问题。对我来说,这是一个与react-native-push-notification相关的问题,当我在错误的地方调用.configure时,这个问题会发生在我的安卓模拟器中。“如果您确实在那里调用.configure,通知处理程序将不会触发”。在根据文档加载任何组件之前,有必要进行调用。

请注意,iOS模拟器只是缺少一些功能,因此根据Platform.OS of react-native处理您认为合适的模拟或替代方案。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63772643

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档