ios - 在 ios 上打开推送通知时反应本机应用程序崩溃
问题描述
我在旧应用程序中实现推送通知react-native-push-notification
。
Android 工作正常,但 iOS 在点击推送时崩溃并出现以下错误:
由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“+[RCTConvert RCTFormatUNNotificationResponse:]:无法识别的选择器发送到类 0x10351f4a8”以未捕获的 NSException 类型异常终止
AppDelegate.m
#import "AppDelegate.h"
#import <React/RCTBridge.h>
#import <React/RCTBundleURLProvider.h>
#import <React/RCTRootView.h>
#import <UserNotifications/UserNotifications.h>
#import <RNCPushNotificationIOS.h>
#import "RNSplashScreen.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
RCTBridge *bridge = [[RCTBridge alloc] initWithDelegate:self launchOptions:launchOptions];
RCTRootView *rootView = [[RCTRootView alloc] initWithBridge:bridge
moduleName:@"appName"
initialProperties:nil];
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
center.delegate = self;
rootView.backgroundColor = [[UIColor alloc] initWithRed:1.0f green:1.0f blue:1.0f alpha:1];
self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
UIViewController *rootViewController = [UIViewController new];
rootViewController.view = rootView;
self.window.rootViewController = rootViewController;
[self.window makeKeyAndVisible];
[RNSplashScreen show];
return YES;
}
//Called when a notification is delivered to a foreground app.
-(void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler
{
completionHandler(UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge);
}
// 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 localNotification event
- (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response
withCompletionHandler:(void (^)(void))completionHandler
{
[RNCPushNotificationIOS didReceiveNotificationResponse:response];
}
- (NSURL *)sourceURLForBridge:(RCTBridge *)bridge
{
#if DEBUG
return [[RCTBundleURLProvider sharedSettings] jsBundleURLForBundleRoot:@"index" fallbackResource:nil];
#else
return [[NSBundle mainBundle] URLForResource:@"main" withExtension:@"jsbundle"];
#endif
}
@end
套餐:
"react": "16.8.3",
"react-native": "0.59.9",
"react-native-push-notification": "8.1.0",
"@react-native-community/push-notification-ios": "^1.10.1"
解决方案
推荐阅读
- javascript - 从javascript为隐藏字段赋值
- python - Python 类字符串类型参数显示为浮点类型
- spring-boot - 如何在 Spring Boot 中运行应用程序期间切换数据库源存储库
- javascript - react app如何实时更新数据
- c++ - 没有参数列表的模板名称“事件”的无效使用,模板问题
- azure - 来自 lex 中单个 lamda 函数的多个返回语句
- python - 如何在“为什么”循环中添加“如果”命令
- django - 覆盖 Django 序列化程序中的“创建”函数
- apache - 不存在的子域被 Google 索引 - 通过 .htaccess 删除僵尸子域
- c - 如何将文本逐行存储到C中的二维数组中?