经过数小时的调试,似乎有些版本不能很好地相互配合,我已经设法修复了错误“不变违规:本机模块不能为空”并让 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 步:链接 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 组下):
通过选择 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>
将 AppDelegate.m 中 RCTPushNotificationManager 的所有条目替换为 RNCPushNotificationIOS
然后,根据react-native-community.push-notification-ios
,在
@end 之前添加以下代码 sn-p
// 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
希望这对某人有所帮助!