【问题标题】:(React Native - Using AWS Amplify) - Invariant Violation: Native module cannot be null(React Native - 使用 AWS Amplify) - 不变违规:本机模块不能为空
【发布时间】:2020-06-02 16:16:11
【问题描述】:

问题描述:

我正在使用 AWS Amplify doc 在我的 React-Native 应用程序中实现推送通知,并且使用 iOS 部分进行测试失败并出现错误“Invariant Violation: Native module cannot be null”,但是如果我测试(获取设备令牌并发送推送通知)它工作的 Android 部分。我在 iOS 上看到的错误截图如下:

到目前为止我已经尝试过什么:

  1. 将 react-native 版本从 0.59.10 升级到 0.61.5
  2. 根据github post,我还尝试安装以下内容:

    @react-native-community/push-notification-ios

    npm install aws-amplify@unstable

此模块 (aws-amplify@unstable) 引入了错误 ????说 TypeError: undefined is not an object (evalating '_core.Amplify.register') 所以我决定摆脱它。

  1. 使用 "@aws-amplify/pushnotification": "^3.0.13" 的最新软件包适用于 Android,但在 iOS 中我又回到了原来的错误 ???? : "Invariant Violation: Native module cannot be null"

目前我的 package.json 如下:

"dependencies": {
"@aws-amplify/pushnotification": "^1.1.4",
"@aws-amplify/analytics": "^1.3.3",
"@react-native-community/netinfo": "^5.7.0",
"@react-native-community/push-notification-ios": "^1.2.0",
"amazon-cognito-identity-js": "^4.2.1",
"aws-amplify": "^1.2.4",
"aws-amplify-react-native": "^4.2.0",
"axios": "^0.19.2",
"cache": "^2.3.1",
"react": "16.9.0",
"react-native": "^0.62.2"
}

我先睡吧,明天早上继续调试..

【问题讨论】:

    标签: react-native aws-amplify aws-pinpoint


    【解决方案1】:

    经过数小时的调试,似乎有些版本不能很好地相互配合,我已经设法修复了错误“不变违规:本机模块不能为空”并让 Android 和 iOS 推送通知正常工作??使用以下版本 aws amplify lib 和 @react-native-community/push-notification-ios:

    "dependencies": {
    "@aws-amplify/pushnotification": "^3.0.13",
    "@aws-amplify/analytics": "^1.3.3",
    "@react-native-community/netinfo": "^5.7.0",
    "@react-native-community/push-notification-ios": "^1.0.2",
    "amazon-cognito-identity-js": "^4.2.1",
    "aws-amplify": "^3.0.13",
    "aws-amplify-react-native": "^4.2.0",
    "axios": "^0.19.2",
    "cache": "^2.3.1",
    "react": "16.9.0",
    "react-native": "^0.62.2"
    },
    

    "dependencies": {
    "@react-native-community/push-notification-ios": "^1.2.0",
    "@react-native-community/netinfo": "^5.7.0",
    "@aws-amplify/pushnotification": "^3.1.2",
    "@aws-amplify/analytics": "^1.3.3",
    "@aws-amplify/core": "^3.3.2",
    "amazon-cognito-identity-js": "^4.2.1",
    "aws-amplify-react-native": "^4.2.0",
    "aws-amplify": "^3.0.16",
    "axios": "^0.19.2",
    "cache": "^2.3.1",
    "react": "16.9.0",
    "react-native": "^0.62.2"
    },
    

    AWS Amplify(iOS 推送通知模块)似乎已从 react-native 核心切换到 @react-native-community/push-notification-ios。因此,由于此迁移,这里有一些更改,如果您遇到此问题,可能需要交叉检查:

    第 1 步:更新 Podfile

    从您的 Podfile(您可以在 ios 文件夹中找到)中删除 'React-RCTPushNotification'。:

    pod 'React-RCTPushNotification', :path => '../node_modules/react-native/Libraries/PushNotificationIOS'
    

    第 2 步:链接 PushNotificationIOS 库

    第 2.1 步:自动链接

    将以下 RNCPushNotificationIOS 添加到您的 podfile(您可以在 ios 文件夹中找到)。

    pod 'RNCPushNotificationIOS', :path => '../node_modules/@react-native-community/push-notification-ios/RNCPushNotificationIOS.podspec'
    

    然后通过运行以下命令安装 pod 依赖项:cd ios && pod install

    第 2.2 步:手动链接(如果自动链接对您不起作用,请考虑使用此选项)

    将这个 PushNotificationIOS.xcodeproj 文件 (node_modules/@react-native-community/push-notification-ios/ios) 拖到 Xcode 上的项目中(通常在 Xcode 的 Libraries 组下):

    通过选择 Project Navigator -> Target -> Build Phrases -> Linked Binary with Libraries,将 libRNCPushNotificationIOS.a 添加到您的链接二进制文件中(确保 libRNCPushNotificationIOS.a 存在)

    第 3 步:增强 AppDelegate

    第 3.1 步:更新 AppDelegate.h

    在文件顶部添加以下内容:

    #import <UserNotifications/UNUserNotificationCenter.h>
    

    然后,将“UNUserNotificationCenterDelegate”添加到协议中,如下所示:

    @interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate, UNUserNotificationCenterDelegate>
    

    第 3.2 步:更新 AppDelegate.m

    在文件顶部添加以下内容:

    #import <UserNotifications/UserNotifications.h>
    #import <RNCPushNotificationIOS.h>
    

    将 AppDelegate.m 中 RCTPushNotificationManager 的所有条目替换为 RNCPushNotificationIOS

    然后,根据react-native-community.push-notification-ios

    ,在 @end 之前添加以下代码 sn-p
    // 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];
    }
    // Required for the localNotification event.
    - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
    {
      [RNCPushNotificationIOS didReceiveLocalNotification:notification];
    }
    

    其他有用的提示!

    每当您更新 package.json 时,请执行以下操作:

    rm -rf -rf node_modules
    yarn cache clean --force
    yarn install
    cd ios && pod install
    React-native start -- --reset-cache
    

    希望这对某人有所帮助!

    【讨论】:

      猜你喜欢
      • 2020-12-13
      • 1970-01-01
      • 2019-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-07
      • 1970-01-01
      相关资源
      最近更新 更多