问题描述:
我正在使用此AWS Amplify doc在我的React-Native应用程序中实现推送通知,并且对iOS部件的测试失败,并显示错误“ Invariant Violation:Native module不能为空”,但是,如果我测试(即获取设备令牌并发送推送通知),则Android部分可以工作。我在iOS上看到的错误的屏幕截图如下:
到目前为止我已经尝试过:
根据此github post,我还尝试安装以下内容:
@ react-native-community / push-notification-ios
npm install aws-amplify @ unstable
此模块( aws-amplify @ unstable )引入了错误?,提示 TypeError:未定义不是对象(评估'_core.Amplify.register') ,所以我决定摆脱它。
当前,我按如下所示离开了package.json:
type
让我睡一会儿,明天早上我将继续调试..
答案 0 :(得分:2)
经过数小时的调试,看来某些版本不能很好地相互配合,并且我设法解决了“不变违规:本机模块不能为空”的错误,并使用以下命令使Android和iOS推送通知正常工作以下版本的aws会放大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中删除'React-RCTPushNotification'(您可以在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组下):< / p>
通过选择“项目导航器”->“目标”->“构建短语”->“与库链接的二进制文件”,确保将 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>
将 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];
}
每当更新package.json时,请执行以下操作:
rm -rf -rf node_modules
yarn cache clean --force
yarn install
cd ios && pod install
React-native start -- --reset-cache
希望这对某人有帮助!