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

syu*_*maK 8 react-native aws-pinpoint aws-amplify

问题说明:

我正在使用此AWS Amplify 文档在我的 React-Native 应用程序中实现推送通知,并且使用 iOS 部分进行测试失败并显示错误“不变违规:本机模块不能为空”,但是如果我测试(获取设备令牌并发送推送通知)它工作的 Android 部分。我在 iOS 上看到的错误截图如下所示:

在此处输入图片说明

到目前为止我尝试过的:

  1. 将 react-native 版本从 0.59.10 升级到 0.61.5
  2. 根据this 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中我又回到原来的错误:“不变违规:本机模块不能为空

目前我已经离开了我的 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"
}
Run Code Online (Sandbox Code Playgroud)

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

syu*_*maK 7

经过几个小时的调试后,似乎有些版本彼此不能很好地配合,我已经成功修复了错误“Invariant Violation:本机模块不能为空”,并使用以下版本 aws amplify 使 Android 和 iOS 推送通知正常工作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"
},
Run Code Online (Sandbox Code Playgroud)

或者

"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"
},
Run Code Online (Sandbox Code Playgroud)

AWS Amplify(适用于 iOS 的推送通知模块)似乎已从 React-native 核心切换到 @react-native-community/push-notification-ios。因此,由于此迁移,可能需要进行一些更改,以防您遇到此问题:

第 1 步:更新 Podfile

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

pod 'React-RCTPushNotification', :path => '../node_modules/react-native/Libraries/PushNotificationIOS'
Run Code Online (Sandbox Code Playgroud)

步骤 2:链接 PushNotificationIOS 库

步骤2.1:自动链接

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

pod 'RNCPushNotificationIOS', :path => '../node_modules/@react-native-community/push-notification-ios/RNCPushNotificationIOS.podspec'
Run Code Online (Sandbox Code Playgroud)

然后通过运行以下命令安装 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>
Run Code Online (Sandbox Code Playgroud)

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

@interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate, UNUserNotificationCenterDelegate>
Run Code Online (Sandbox Code Playgroud)

步骤3.2:更新AppDelegate.m

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

#import <UserNotifications/UserNotifications.h>
#import <RNCPushNotificationIOS.h>
Run Code Online (Sandbox Code Playgroud)

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

然后,根据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];
}
Run Code Online (Sandbox Code Playgroud)

其他有用的提示!

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

rm -rf -rf node_modules
yarn cache clean --force
yarn install
cd ios && pod install
React-native start -- --reset-cache
Run Code Online (Sandbox Code Playgroud)

希望这对某人有帮助!