问题描述:
我正在使用此AWS Amplify doc在我的React-Native应用程序中实现推送通知,并且对iOS部件的测试失败,并显示错误“ Invariant Violation:Native module不能为null”,但是,如果我进行测试(< [ie获取设备令牌并发送推送通知)。我在iOS上看到的错误的屏幕截图如下:
到目前为止我尝试过的:
@ react-native-community / push-notification-ios
npm install aws-amplify @ unstable
此模块(
aws-amplify @ unstable
)引入了一个错误😓说TypeError:undefined不是一个对象(正在评估'_core.Amplify.register'],所以我决定摆脱它。"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"
}
让我睡一会,明天早上我将继续调试..
"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组下:)]通过选择项目导航器->目标->构建短语->与库链接的二进制文件(确保存在libRNCPushNotificationIOS.a),将[[ 步骤3:Augment AppDelegate 步骤3.1:更新AppDelegate.h
UNUserNotificationCenterDelegate#import <UserNotifications/UNUserNotificationCenter.h>
然后,将'
@interface AppDelegate : UIResponder <UIApplicationDelegate, RCTBridgeDelegate, UNUserNotificationCenterDelegate>
步骤3.2:更新AppDelegate.m
在文件顶部添加以下内容:
RNCPushNotificationIOS#import <UserNotifications/UserNotifications.h>
#import <RNCPushNotificationIOS.h>
用RCTPushNotificationManager
然后,根据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
希望这对某人有帮助!