gpt4 book ai didi

react-native - (React Native - 使用 AWS Amplify) - 不变违规 : Native module cannot be null

转载 作者:行者123 更新时间:2023-12-03 17:00:00 25 4
gpt4 key购买 nike

问题描述:

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

enter image description here

到目前为止我所尝试的:

  • 将 react-native 版本从 0.59.10 升级到 0.61.5
  • 据此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') 所以我决定摆脱它。
  • 使用 的最新软件包“@aws-amplify/pushnotification”:“^3.0.13”适用于 Android,但在 iOS 中,我又回到了原来的错误😓:“ 不变违规: native 模块不能为空

  • 目前我的 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"
    }

    让我睡一觉,明天早上继续调试..

    最佳答案

    经过数小时的调试,似乎有些版本不能很好地相互配合,我已经设法修复了错误“不变违规: native 模块不能为空”并让 Android 和 iOS 推送通知正常工作😊💪使用以下版本aws 放大库和@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

    删除 ' react RCTPushNotification' 从您的 Podfile (您可以在 ios 文件夹中找到)。:
    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 组下):

    enter image description here

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

    enter image description here
    enter image description here

    第 3 步:增强 AppDelegate

    步骤 3.1:更新 AppDelegate.h

    在文件顶部添加以下内容:
    #import <UserNotifications/UNUserNotificationCenter.h>

    然后,添加 ' UNUserNotificationCenterDelegate ' 到如下所示的协议(protocol):
    @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
    // 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];
    }

    其他有用的提示!

    whenever you update your package.json, do the following :


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

    希望这对某人有帮助!

    关于react-native - (React Native - 使用 AWS Amplify) - 不变违规 : Native module cannot be null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62156486/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com