[notifications][ios] Fix crashes when notifications are received in background on iOS 9 #952

This commit is contained in:
Chris Bianca 2018-04-10 15:16:44 +01:00
parent 6edb37b0ba
commit 5127486712
1 changed files with 14 additions and 14 deletions

View File

@ -23,6 +23,7 @@ static RNFirebaseNotifications *theRNFirebaseNotifications = nil;
// static NSMutableArray *pendingEvents = nil; // static NSMutableArray *pendingEvents = nil;
static NSDictionary *initialNotification = nil; static NSDictionary *initialNotification = nil;
static bool jsReady = FALSE; static bool jsReady = FALSE;
static NSString *const DEFAULT_ACTION = @"com.apple.UNNotificationDefaultActionIdentifier";
+ (nonnull instancetype)instance { + (nonnull instancetype)instance {
return theRNFirebaseNotifications; return theRNFirebaseNotifications;
@ -47,9 +48,9 @@ RCT_EXPORT_MODULE();
- (void)initialise { - (void)initialise {
// If we're on iOS 10 then we need to set this as a delegate for the UNUserNotificationCenter // If we're on iOS 10 then we need to set this as a delegate for the UNUserNotificationCenter
#if defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0 if (@available(iOS 10.0, *)) {
[UNUserNotificationCenter currentNotificationCenter].delegate = self; [UNUserNotificationCenter currentNotificationCenter].delegate = self;
#endif }
// Set static instance for use from AppDelegate // Set static instance for use from AppDelegate
theRNFirebaseNotifications = self; theRNFirebaseNotifications = self;
@ -86,7 +87,7 @@ RCT_EXPORT_MODULE();
NSDictionary *notification = [self parseUILocalNotification:localNotification]; NSDictionary *notification = [self parseUILocalNotification:localNotification];
if (event == NOTIFICATIONS_NOTIFICATION_OPENED) { if (event == NOTIFICATIONS_NOTIFICATION_OPENED) {
notification = @{ notification = @{
@"action": UNNotificationDefaultActionIdentifier, @"action": DEFAULT_ACTION,
@"notification": notification @"notification": notification
}; };
} }
@ -125,7 +126,7 @@ RCT_EXPORT_MODULE();
// For onOpened events, we set the default action name as iOS 8/9 has no concept of actions // For onOpened events, we set the default action name as iOS 8/9 has no concept of actions
if (event == NOTIFICATIONS_NOTIFICATION_OPENED) { if (event == NOTIFICATIONS_NOTIFICATION_OPENED) {
notification = @{ notification = @{
@"action": UNNotificationDefaultActionIdentifier, @"action": DEFAULT_ACTION,
@"notification": notification @"notification": notification
}; };
} }
@ -147,8 +148,7 @@ RCT_EXPORT_MODULE();
// Handle incoming notification messages while app is in the foreground. // Handle incoming notification messages while app is in the foreground.
- (void)userNotificationCenter:(UNUserNotificationCenter *)center - (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler NS_AVAILABLE_IOS(10_0) {
UNNotificationTrigger *trigger = notification.request.trigger; UNNotificationTrigger *trigger = notification.request.trigger;
BOOL isFcm = trigger && [notification.request.trigger class] == [UNPushNotificationTrigger class]; BOOL isFcm = trigger && [notification.request.trigger class] == [UNPushNotificationTrigger class];
BOOL isScheduled = trigger && [notification.request.trigger class] == [UNCalendarNotificationTrigger class]; BOOL isScheduled = trigger && [notification.request.trigger class] == [UNCalendarNotificationTrigger class];
@ -187,9 +187,9 @@ RCT_EXPORT_MODULE();
- (void)userNotificationCenter:(UNUserNotificationCenter *)center - (void)userNotificationCenter:(UNUserNotificationCenter *)center
didReceiveNotificationResponse:(UNNotificationResponse *)response didReceiveNotificationResponse:(UNNotificationResponse *)response
#if defined(__IPHONE_11_0) #if defined(__IPHONE_11_0)
withCompletionHandler:(void(^)(void))completionHandler { withCompletionHandler:(void(^)(void))completionHandler NS_AVAILABLE_IOS(10_0) {
#else #else
withCompletionHandler:(void(^)())completionHandler { withCompletionHandler:(void(^)())completionHandler NS_AVAILABLE_IOS(10_0) {
#endif #endif
NSDictionary *message = [self parseUNNotificationResponse:response]; NSDictionary *message = [self parseUNNotificationResponse:response];
@ -273,13 +273,13 @@ RCT_EXPORT_METHOD(getInitialNotification:(RCTPromiseResolveBlock)resolve rejecte
} else if (self.bridge.launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) { } else if (self.bridge.launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]) {
UILocalNotification *localNotification = self.bridge.launchOptions[UIApplicationLaunchOptionsLocalNotificationKey]; UILocalNotification *localNotification = self.bridge.launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
resolve(@{ resolve(@{
@"action": UNNotificationDefaultActionIdentifier, @"action": DEFAULT_ACTION,
@"notification": [self parseUILocalNotification:localNotification] @"notification": [self parseUILocalNotification:localNotification]
}); });
} else if (self.bridge.launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) { } else if (self.bridge.launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]) {
NSDictionary *remoteNotification = [self bridge].launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]; NSDictionary *remoteNotification = [self bridge].launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
resolve(@{ resolve(@{
@"action": UNNotificationDefaultActionIdentifier, @"action": DEFAULT_ACTION,
@"notification": [self parseUserInfo:remoteNotification] @"notification": [self parseUserInfo:remoteNotification]
}); });
} else { } else {
@ -456,7 +456,7 @@ RCT_EXPORT_METHOD(jsInitialised:(RCTPromiseResolveBlock)resolve rejecter:(RCTPro
} }
- (UNNotificationRequest*) buildUNNotificationRequest:(NSDictionary *) notification - (UNNotificationRequest*) buildUNNotificationRequest:(NSDictionary *) notification
withSchedule:(BOOL) withSchedule { withSchedule:(BOOL) withSchedule NS_AVAILABLE_IOS(10_0) {
UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init]; UNMutableNotificationContent *content = [[UNMutableNotificationContent alloc] init];
if (notification[@"body"]) { if (notification[@"body"]) {
content.body = notification[@"body"]; content.body = notification[@"body"];
@ -591,7 +591,7 @@ RCT_EXPORT_METHOD(jsInitialised:(RCTPromiseResolveBlock)resolve rejecter:(RCTPro
return notification; return notification;
} }
- (NSDictionary*)parseUNNotificationResponse:(UNNotificationResponse *)response { - (NSDictionary*)parseUNNotificationResponse:(UNNotificationResponse *)response NS_AVAILABLE_IOS(10_0) {
NSMutableDictionary *notificationResponse = [[NSMutableDictionary alloc] init]; NSMutableDictionary *notificationResponse = [[NSMutableDictionary alloc] init];
NSDictionary *notification = [self parseUNNotification:response.notification]; NSDictionary *notification = [self parseUNNotification:response.notification];
notificationResponse[@"notification"] = notification; notificationResponse[@"notification"] = notification;
@ -600,11 +600,11 @@ RCT_EXPORT_METHOD(jsInitialised:(RCTPromiseResolveBlock)resolve rejecter:(RCTPro
return notificationResponse; return notificationResponse;
} }
- (NSDictionary*)parseUNNotification:(UNNotification *)notification { - (NSDictionary*)parseUNNotification:(UNNotification *)notification NS_AVAILABLE_IOS(10_0) {
return [self parseUNNotificationRequest:notification.request]; return [self parseUNNotificationRequest:notification.request];
} }
- (NSDictionary*) parseUNNotificationRequest:(UNNotificationRequest *) notificationRequest { - (NSDictionary*) parseUNNotificationRequest:(UNNotificationRequest *) notificationRequest NS_AVAILABLE_IOS(10_0) {
NSMutableDictionary *notification = [[NSMutableDictionary alloc] init]; NSMutableDictionary *notification = [[NSMutableDictionary alloc] init];
notification[@"notificationId"] = notificationRequest.identifier; notification[@"notificationId"] = notificationRequest.identifier;