(本机反应-使用AWS Amplify)-不变违规:本机模块不能为空 [英] (React Native - Using AWS Amplify) - Invariant Violation: Native module cannot be null
问题描述
问题描述:
我正在使用这个AWS Amplify doc在我的Reaction-Native应用程序中实现推送通知,iOS部分的测试失败,并出现错误不变违规:本地模块不能为空,但如果我测试(即获取设备令牌并发送推送通知)Android部分,它可以工作。我在iOS上看到的错误截图如下:
到目前为止我尝试的内容:
- 将REACT本机版本从0.59.10升级到0.61.5
根据这个github post,我还尝试安装了以下程序:
@react-native-community/push-notification-ios
NPM安装AWS-Amplify@不稳定
此模块(aws-Amplify@unstant)引入了一个错误😓,指出类型错误:未定义的不是对象(计算‘_core.Amplify.Register’)所以我决定删除它。
- 使用最新的包"@aws-Amplify/Push Notify":"^3.0.13"适用于安卓系统,但在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"
}
让我睡一觉,明天早上我会继续调试..
推荐答案
经过几个小时的调试,一些版本似乎不能很好地相互配合,我已经设法修复了错误"不变违规:本机模块不能为空",并使用以下版本AWS Amplify lib和@react-native-community/push-notification-ios:
获得了Android和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原生内核切换到@react-native-community/push-notification-ios.因此,以下是此迁移的一些更改,您可能需要交叉检查这些更改,以防遇到此问题:
第1步:更新Podfile
从您的Podfile(您可以在iOS文件夹中找到)中删除‘Reaction-RCTPushNotification’。:
pod 'React-RCTPushNotification', :path => '../node_modules/react-native/Libraries/PushNotificationIOS'
第二步:链接PushNotificationIOS库
步骤2.1:自动链接
将以下RNCPushNotificationIOS添加到您的Pod文件(可以在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上的库组下):
通过选择项目导航器->目标->构建短语->链接二进制文件与库(确保libRNCPushNotificationIOS.a已存在),将libRNCPushNotificationIOS.a添加到链接的二进制文件中。
第三步:扩充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>
将RCTPushNotificationManager的AppDelegate.m中的所有条目替换为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];
}
其他有用的提示!
每次更新Package.json时,请执行以下操作:
rm -rf -rf node_modules
yarn cache clean --force
yarn install
cd ios && pod install
React-native start -- --reset-cache
希望这能帮助某人!
这篇关于(本机反应-使用AWS Amplify)-不变违规:本机模块不能为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!