[fcm] iOS 8/9 support

This commit is contained in:
Chris Bianca 2018-02-05 09:18:53 +00:00
parent 3e7a1efe4e
commit b71a2c7aec
1 changed files with 46 additions and 34 deletions

View File

@ -52,10 +52,7 @@ RCT_EXPORT_MODULE()
#endif #endif
// Set static instance for use from AppDelegate // Set static instance for use from AppDelegate
static dispatch_once_t once;
dispatch_once(&once, ^{
theRNFirebaseMessaging = self; theRNFirebaseMessaging = self;
});
} }
- (void)dealloc { - (void)dealloc {
@ -67,18 +64,20 @@ RCT_EXPORT_MODULE()
// Listen for background messages // Listen for background messages
- (void)didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo { - (void)didReceiveRemoteNotification:(nonnull NSDictionary *)userInfo {
BOOL isFromBackground = (RCTSharedApplication().applicationState == UIApplicationStateInactive); BOOL isFromBackground = (RCTSharedApplication().applicationState == UIApplicationStateInactive);
NSDictionary *message = [self parseUserInfo:userInfo clickAction:nil openedFromTray:isFromBackground];
// TODO: Format data before send [RNFirebaseUtil sendJSEvent:self name:MESSAGING_MESSAGE_RECEIVED body:message];
[RNFirebaseUtil sendJSEvent:self name:MESSAGING_MESSAGE_RECEIVED body:userInfo];
} }
// Listen for background messages // Listen for background messages
- (void)didReceiveRemoteNotification:(NSDictionary *)userInfo - (void)didReceiveRemoteNotification:(NSDictionary *)userInfo
fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler { fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
BOOL isFromBackground = (RCTSharedApplication().applicationState == UIApplicationStateInactive); BOOL isFromBackground = (RCTSharedApplication().applicationState == UIApplicationStateInactive);
NSDictionary *message = [self parseUserInfo:userInfo clickAction:nil openedFromTray:isFromBackground];
// TODO: Format data before send [RNFirebaseUtil sendJSEvent:self name:MESSAGING_MESSAGE_RECEIVED body:message];
[RNFirebaseUtil sendJSEvent:self name:MESSAGING_MESSAGE_RECEIVED body:userInfo];
// TODO: FetchCompletionHandler?
} }
// ** UNUserNotificationCenterDelegate methods ** // ** UNUserNotificationCenterDelegate methods **
@ -105,9 +104,9 @@ didReceiveNotificationResponse:(UNNotificationResponse *)response
#endif #endif
NSDictionary *userInfo = [self parseUNNotification:response.notification openedFromTray:true]; NSDictionary *userInfo = [self parseUNNotification:response.notification openedFromTray:true];
// TODO: Format data before send
[RNFirebaseUtil sendJSEvent:self name:MESSAGING_MESSAGE_RECEIVED body:userInfo]; [RNFirebaseUtil sendJSEvent:self name:MESSAGING_MESSAGE_RECEIVED body:userInfo];
// TODO: Validate this
completionHandler(); completionHandler();
} }
@ -209,22 +208,34 @@ RCT_EXPORT_METHOD(unsubscribeFromTopic: (NSString*) topic) {
} else if ([k1 isEqualToString:@"from"]) { } else if ([k1 isEqualToString:@"from"]) {
message[@"from"] = appData[k1]; message[@"from"] = appData[k1];
} else if ([k1 isEqualToString:@"notification"]) { } else if ([k1 isEqualToString:@"notification"]) {
NSDictionary *n = appData[k1]; NSDictionary *notification = appData[k1];
NSMutableDictionary *notification = [[NSMutableDictionary alloc] init]; NSMutableDictionary *notif = [[NSMutableDictionary alloc] init];
for (id k2 in appData[@"notification"]) { for (id k2 in notification) {
if ([k2 isEqualToString:@"badge"]) { if ([k2 isEqualToString:@"badge"]) {
notification[@"badge"] = n[k2]; notif[@"badge"] = notification[k2];
} else if ([k2 isEqualToString:@"body"]) { } else if ([k2 isEqualToString:@"body"]) {
notification[@"body"] = n[k2]; notif[@"body"] = notification[k2];
} else if ([k2 isEqualToString:@"body_loc_args"]) {
notif[@"bodyLocalizationArgs"] = notification[k2];
} else if ([k2 isEqualToString:@"body_loc_key"]) {
notif[@"bodyLocalizationKey"] = notification[k2];
} else if ([k2 isEqualToString:@"click_action"]) {
notif[@"clickAction"] = notification[k2];
} else if ([k2 isEqualToString:@"sound"]) { } else if ([k2 isEqualToString:@"sound"]) {
notification[@"sound"] = n[k2]; notif[@"sound"] = notification[k2];
} else if ([k2 isEqualToString:@"subtitle"]) {
notif[@"subtitle"] = notification[k2];
} else if ([k2 isEqualToString:@"title"]) { } else if ([k2 isEqualToString:@"title"]) {
notification[@"title"] = n[k2]; notif[@"title"] = notification[k2];
} else if ([k2 isEqualToString:@"title_loc_args"]) {
notif[@"titleLocalizationArgs"] = notification[k2];
} else if ([k2 isEqualToString:@"title_loc_key"]) {
notif[@"titleLocalizationKey"] = notification[k2];
} else { } else {
NSLog(@"Unknown notification key: %@", k2); NSLog(@"Unknown notification key: %@", k2);
} }
} }
message[@"notification"] = notification; message[@"notification"] = notif;
} else { } else {
// Assume custom data key // Assume custom data key
data[k1] = appData[k1]; data[k1] = appData[k1];
@ -329,3 +340,4 @@ RCT_EXPORT_METHOD(unsubscribeFromTopic: (NSString*) topic) {
@implementation RNFirebaseMessaging @implementation RNFirebaseMessaging
@end @end
#endif #endif