[fcm] iOS 10 support for messaging

This commit is contained in:
Chris Bianca 2018-02-02 17:16:55 +00:00
parent a460d8f809
commit 3e7a1efe4e
1 changed files with 140 additions and 52 deletions

View File

@ -47,9 +47,9 @@ RCT_EXPORT_MODULE()
[FIRMessaging messaging].shouldEstablishDirectChannel = YES; [FIRMessaging messaging].shouldEstablishDirectChannel = YES;
// 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 defined(__IPHONE_10_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_10_0
[UNUserNotificationCenter currentNotificationCenter].delegate = self; [UNUserNotificationCenter currentNotificationCenter].delegate = self;
#endif #endif
// Set static instance for use from AppDelegate // Set static instance for use from AppDelegate
static dispatch_once_t once; static dispatch_once_t once;
@ -87,10 +87,9 @@ RCT_EXPORT_MODULE()
- (void)userNotificationCenter:(UNUserNotificationCenter *)center - (void)userNotificationCenter:(UNUserNotificationCenter *)center
willPresentNotification:(UNNotification *)notification willPresentNotification:(UNNotification *)notification
withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler {
NSDictionary *userInfo = notification.request.content.userInfo; NSDictionary *message = [self parseUNNotification:notification openedFromTray:false];
// TODO: Format data before send [RNFirebaseUtil sendJSEvent:self name:MESSAGING_MESSAGE_RECEIVED body:message];
[RNFirebaseUtil sendJSEvent:self name:MESSAGING_MESSAGE_RECEIVED body:userInfo];
// TODO: Change this to your preferred presentation option // TODO: Change this to your preferred presentation option
completionHandler(UNNotificationPresentationOptionNone); completionHandler(UNNotificationPresentationOptionNone);
@ -104,7 +103,7 @@ didReceiveNotificationResponse:(UNNotificationResponse *)response
#else #else
withCompletionHandler:(void(^)())completionHandler { withCompletionHandler:(void(^)())completionHandler {
#endif #endif
NSDictionary *userInfo = response.notification.request.content.userInfo; NSDictionary *userInfo = [self parseUNNotification:response.notification openedFromTray:true];
// TODO: Format data before send // TODO: Format data before send
[RNFirebaseUtil sendJSEvent:self name:MESSAGING_MESSAGE_RECEIVED body:userInfo]; [RNFirebaseUtil sendJSEvent:self name:MESSAGING_MESSAGE_RECEIVED body:userInfo];
@ -124,38 +123,7 @@ didReceiveNotificationResponse:(UNNotificationResponse *)response
// Listen for data messages in the foreground // Listen for data messages in the foreground
- (void)applicationReceivedRemoteMessage:(nonnull FIRMessagingRemoteMessage *)remoteMessage { - (void)applicationReceivedRemoteMessage:(nonnull FIRMessagingRemoteMessage *)remoteMessage {
NSDictionary *appData = remoteMessage.appData; NSDictionary *message = [self parseFIRMessagingRemoteMessage:remoteMessage openedFromTray:false];
NSMutableDictionary *message = [[NSMutableDictionary alloc] init];
NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
for (id k1 in appData) {
if ([k1 isEqualToString:@"collapse_key"]) {
message[@"collapseKey"] = appData[@"collapse_key"];
} else if ([k1 isEqualToString:@"from"]) {
message[@"from"] = appData[@"from"];
} else if ([k1 isEqualToString:@"notification"]) {
NSDictionary *n = appData[@"notification"];
NSMutableDictionary *notification = [[NSMutableDictionary alloc] init];
for (id k2 in appData[@"notification"]) {
if ([k2 isEqualToString:@"badge"]) {
notification[@"badge"] = n[@"badge"];
} else if ([k2 isEqualToString:@"body"]) {
notification[@"body"] = n[@"body"];
} else if ([k2 isEqualToString:@"sound"]) {
notification[@"sound"] = n[@"sound"];
} else if ([k2 isEqualToString:@"title"]) {
notification[@"title"] = n[@"title"];
} else {
NSLog(@"Unknown notification key: %@", k2);
}
}
message[@"notification"] = notification;
} else {
data[k1] = appData[k1];
}
}
message[@"data"] = data;
message[@"openedFromTray"] = @(false);
[RNFirebaseUtil sendJSEvent:self name:MESSAGING_MESSAGE_RECEIVED body:message]; [RNFirebaseUtil sendJSEvent:self name:MESSAGING_MESSAGE_RECEIVED body:message];
} }
@ -206,15 +174,16 @@ RCT_EXPORT_METHOD(getBadge: (RCTPromiseResolveBlock)resolve rejecter:(RCTPromise
RCT_EXPORT_METHOD(getInitialMessage:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){ RCT_EXPORT_METHOD(getInitialMessage:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject){
NSDictionary *notification = [self bridge].launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]; NSDictionary *notification = [self bridge].launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey];
if (notification) { if (notification) {
resolve([notification copy]); NSDictionary *message = [self parseUserInfo:notification clickAction:nil openedFromTray:true];
resolve(message);
} else { } else {
resolve(nil); resolve(nil);
} }
} }
RCT_EXPORT_METHOD(setBadge: (NSInteger*) number) { RCT_EXPORT_METHOD(setBadge: (NSInteger) number) {
dispatch_async(dispatch_get_main_queue(), ^{ dispatch_async(dispatch_get_main_queue(), ^{
[RCTSharedApplication() setApplicationIconBadgeNumber:*number]; [RCTSharedApplication() setApplicationIconBadgeNumber:number];
}); });
} }
@ -226,6 +195,125 @@ RCT_EXPORT_METHOD(unsubscribeFromTopic: (NSString*) topic) {
[[FIRMessaging messaging] unsubscribeFromTopic:topic]; [[FIRMessaging messaging] unsubscribeFromTopic:topic];
} }
// ** Start internals **
- (NSDictionary*)parseFIRMessagingRemoteMessage:(FIRMessagingRemoteMessage *)remoteMessage
openedFromTray:(bool)openedFromTray {
NSDictionary *appData = remoteMessage.appData;
NSMutableDictionary *message = [[NSMutableDictionary alloc] init];
NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
for (id k1 in appData) {
if ([k1 isEqualToString:@"collapse_key"]) {
message[@"collapseKey"] = appData[@"collapse_key"];
} else if ([k1 isEqualToString:@"from"]) {
message[@"from"] = appData[k1];
} else if ([k1 isEqualToString:@"notification"]) {
NSDictionary *n = appData[k1];
NSMutableDictionary *notification = [[NSMutableDictionary alloc] init];
for (id k2 in appData[@"notification"]) {
if ([k2 isEqualToString:@"badge"]) {
notification[@"badge"] = n[k2];
} else if ([k2 isEqualToString:@"body"]) {
notification[@"body"] = n[k2];
} else if ([k2 isEqualToString:@"sound"]) {
notification[@"sound"] = n[k2];
} else if ([k2 isEqualToString:@"title"]) {
notification[@"title"] = n[k2];
} else {
NSLog(@"Unknown notification key: %@", k2);
}
}
message[@"notification"] = notification;
} else {
// Assume custom data key
data[k1] = appData[k1];
}
}
message[@"data"] = data;
message[@"openedFromTray"] = @(false);
return message;
}
- (NSDictionary*)parseUNNotification:(UNNotification *)notification
openedFromTray:(bool)openedFromTray {
NSDictionary *userInfo = notification.request.content.userInfo;
NSString *clickAction = notification.request.content.categoryIdentifier;
return [self parseUserInfo:userInfo clickAction:clickAction openedFromTray:openedFromTray];
}
- (NSDictionary*)parseUserInfo:(NSDictionary *)userInfo
clickAction:(NSString *) clickAction
openedFromTray:(bool)openedFromTray {
NSMutableDictionary *message = [[NSMutableDictionary alloc] init];
NSMutableDictionary *notif = [[NSMutableDictionary alloc] init];
NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
for (id k1 in userInfo) {
if ([k1 isEqualToString:@"aps"]) {
NSDictionary *aps = userInfo[k1];
for (id k2 in aps) {
if ([k2 isEqualToString:@"alert"]) {
NSDictionary *alert = aps[k2];
for (id k3 in alert) {
if ([k3 isEqualToString:@"body"]) {
notif[@"body"] = alert[k3];
} else if ([k3 isEqualToString:@"loc-args"]) {
notif[@"bodyLocalizationArgs"] = alert[k3];
} else if ([k3 isEqualToString:@"loc-key"]) {
notif[@"bodyLocalizationKey"] = alert[k3];
} else if ([k3 isEqualToString:@"subtitle"]) {
notif[@"subtitle"] = alert[k3];
} else if ([k3 isEqualToString:@"title"]) {
notif[@"title"] = alert[k3];
} else if ([k3 isEqualToString:@"title-loc-args"]) {
notif[@"titleLocalizationArgs"] = alert[k3];
} else if ([k3 isEqualToString:@"title-loc-key"]) {
notif[@"titleLocalizationKey"] = alert[k3];
} else {
NSLog(@"Unknown alert key: %@", k2);
}
}
} else if ([k2 isEqualToString:@"badge"]) {
notif[@"badge"] = aps[k2];
} else if ([k2 isEqualToString:@"category"]) {
notif[@"clickAction"] = aps[k2];
} else if ([k2 isEqualToString:@"sound"]) {
notif[@"sound"] = aps[k2];
} else {
NSLog(@"Unknown aps key: %@", k2);
}
}
} else if ([k1 isEqualToString:@"gcm.message_id"]) {
message[@"messageId"] = userInfo[k1];
} else if ([k1 isEqualToString:@"google.c.a.ts"]) {
message[@"sentTime"] = userInfo[k1];
} else if ([k1 isEqualToString:@"gcm.n.e"]
|| [k1 isEqualToString:@"gcm.notification.sound2"]
|| [k1 isEqualToString:@"google.c.a.c_id"]
|| [k1 isEqualToString:@"google.c.a.c_l"]
|| [k1 isEqualToString:@"google.c.a.e"]
|| [k1 isEqualToString:@"google.c.a.udt"]) {
// Ignore known keys
} else {
// Assume custom data
data[k1] = userInfo[k1];
}
}
if (!notif[@"clickAction"] && clickAction) {
notif[@"clickAction"] = clickAction;
}
message[@"data"] = data;
message[@"notification"] = notif;
message[@"openedFromTray"] = @(openedFromTray);
return message;
}
- (NSArray<NSString *> *)supportedEvents { - (NSArray<NSString *> *)supportedEvents {
return @[MESSAGING_MESSAGE_RECEIVED, MESSAGING_TOKEN_REFRESHED]; return @[MESSAGING_MESSAGE_RECEIVED, MESSAGING_TOKEN_REFRESHED];
} }
@ -238,6 +326,6 @@ RCT_EXPORT_METHOD(unsubscribeFromTopic: (NSString*) topic) {
@end @end
#else #else
@implementation RNFirebaseMessaging @implementation RNFirebaseMessaging
@end @end
#endif #endif