React Native iOS端点击推送通知崩溃 NSInvalidArgumentException错误求助
问题原因分析
这个崩溃是依赖包版本与旧版React Native不兼容导致的:
你当前使用的React Native 0.59.9版本非常老旧,原生层的RCTConvert类不存在高版本才新增的RCTFormatUNNotificationResponse方法。而你安装的react-native-push-notification@8.1.0和@react-native-community/push-notification-ios@1.10.1都是适配高版本RN的包,点击通知时会调用这个不存在的方法,就触发了unrecognized selector崩溃。
崩溃错误原文:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[RCTConvert RCTFormatUNNotificationResponse:]: unrecognized selector sent to class 0x10351f4a8' terminating with uncaught exception of type NSException
解决方法
推荐优先使用第一种方案,操作成本最低:
方案1:降级依赖到兼容RN 0.59的版本
修改package.json里的依赖版本为:
"react-native-push-notification": "3.1.9", "@react-native-community/push-notification-ios": "1.0.3"
修改完成后执行以下操作清除缓存重装依赖:
- 删除项目根目录的
node_modules文件夹、package-lock.json或yarn.lock文件 - 执行
npm install或yarn install重新安装依赖 - 进入ios目录,删除
Pods文件夹、Podfile.lock文件,执行pod install重新安装原生依赖 - 打开Xcode,点击菜单栏
Product > Clean Build Folder清理构建缓存后重新运行项目即可
方案2:手动补充缺失的原生方法(不降级依赖可选)
如果因为功能需求不能降级依赖,可以在iOS项目中手动添加缺失的方法实现:
- 新建
RCTConvert+UNNotification.h头文件,代码如下:
#import <React/RCTConvert.h> #import <UserNotifications/UserNotifications.h> @interface RCTConvert (UNNotification) + (NSDictionary *)RCTFormatUNNotificationResponse:(UNNotificationResponse *)response; @end
- 新建
RCTConvert+UNNotification.m实现文件,代码如下:
#import "RCTConvert+UNNotification.h" @implementation RCTConvert (UNNotification) + (NSDictionary *)RCTFormatUNNotificationResponse:(UNNotificationResponse *)response { UNNotification *notification = response.notification; UNNotificationRequest *request = notification.request; UNNotificationContent *content = request.content; return @{ @"identifier": response.actionIdentifier, @"userText": response.userText ?: @"", @"notification": @{ @"date": @(notification.date.timeIntervalSince1970 * 1000), @"request": @{ @"identifier": request.identifier, @"content": @{ @"title": content.title ?: @"", @"body": content.body ?: @"", @"userInfo": content.userInfo ?: @{} } } } }; } @end
- 把两个文件加入Xcode项目,重新编译运行即可。
内容的提问来源于stack exchange,提问作者Glidarn
相关产品推荐
相关产品推荐

