(React Native - 使用 AWS Amplify) - 不变违规:本机模块不能为空

(React Native - Using AWS Amplify) - Invariant Violation: Native module cannot be null

问题描述:

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

到目前为止我尝试了什么:

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

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

    npm 安装 aws-amplify@unstable

这个模块 (aws-amplify@unstable) 引入了一个错误说 TypeError: undefined is not an object (evaluating '_core.Amplify.register') 所以我决定去掉它。

  1. 使用 "@aws-amplify/pushnotification" 的最新软件包:"^3.0.13" 适用于 Android 但在 iOS 我' m 回到原来的错误:“不变违规:本机模块不能为空

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

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

经过几个小时的调试,似乎有些版本不能很好地相互配合,我已经设法修复错误 "Invariant Violation: Native module cannot be null" 并获得 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:Link PushNotificationIOS 库

步骤 2.1:自动 Linking

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

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

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

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

将此 PushNotificationIOS.xcodeproj 文件 (node_modules/@react-native-community/push-notification-ios/ios) 拖到 [=119 上的项目中=](通常在 Xcode 的图书馆组下):

通过选择 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>

RNCPushNotificationIOS

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

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

@end 之前添加以下代码片段
// 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

希望这对某人有所帮助!