From b86d51f36b59fa0bb1b5e76b8a85dfc6d554ca7a Mon Sep 17 00:00:00 2001 From: Chris Bianca Date: Fri, 16 Feb 2018 09:47:46 +0000 Subject: [PATCH] [fcm] Start considering multiple events --- ios/RNFirebase/RNFirebaseEvents.h | 5 +++ .../messaging/RNFirebaseMessaging.m | 38 +++++++++++++++---- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/ios/RNFirebase/RNFirebaseEvents.h b/ios/RNFirebase/RNFirebaseEvents.h index 26ce5f3e..ac40e86d 100644 --- a/ios/RNFirebase/RNFirebaseEvents.h +++ b/ios/RNFirebase/RNFirebaseEvents.h @@ -38,6 +38,11 @@ static NSString *const MESSAGING_TOKEN_REFRESHED = @"messaging_token_refreshed"; // TODO: Remove static NSString *const MESSAGING_NOTIFICATION_RECEIVED = @"messaging_notification_received"; +// Notifications +static NSString *const NOTIFICATIONS_NOTIFICATION_CLICKED = @"notifications_notification_clicked"; +static NSString *const NOTIFICATIONS_NOTIFICATION_DISPLAYED = @"notifications_notification_displayed"; +static NSString *const NOTIFICATIONS_NOTIFICATION_RECEIVED = @"notifications_notification_received"; + // AdMob static NSString *const ADMOB_INTERSTITIAL_EVENT = @"interstitial_event"; static NSString *const ADMOB_REWARDED_VIDEO_EVENT = @"rewarded_video_event"; diff --git a/ios/RNFirebase/messaging/RNFirebaseMessaging.m b/ios/RNFirebase/messaging/RNFirebaseMessaging.m index 14454ef0..8198a8b7 100644 --- a/ios/RNFirebase/messaging/RNFirebaseMessaging.m +++ b/ios/RNFirebase/messaging/RNFirebaseMessaging.m @@ -113,11 +113,34 @@ RCT_EXPORT_MODULE() - (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler { + + NSString *event; + UNNotificationPresentationOptions options; NSDictionary *message = [self parseUNNotification:notification messageType:@"PresentNotification" openedFromTray:false]; - - [_callbackHandlers setObject:[completionHandler copy] forKey:message[@"messageId"]]; - - [RNFirebaseUtil sendJSEvent:self name:MESSAGING_MESSAGE_RECEIVED body:message]; + + if (isFCM || isScheduled) { + // If background + if (RCTSharedApplication().applicationState == UIApplicationStateInactive) { + // display notification + options = UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound; + // notification_displayed + event = NOTIFICATIONS_NOTIFICATION_DISPLAYED; + } else { + // don't show notification + options = UNNotificationPresentationOptionNone; + // notification_received + event = NOTIFICATIONS_NOTIFICATION_RECEIVED; + } + } else { + // display notification + options = UNNotificationPresentationOptionAlert | UNNotificationPresentationOptionBadge | UNNotificationPresentationOptionSound; + // no event + } + + if (event) { + [RNFirebaseUtil sendJSEvent:self name:event body:message]; + } + completionHandler(options); } // Handle notification messages after display notification is tapped by the user. @@ -129,10 +152,9 @@ didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)())completionHandler { #endif NSDictionary *message = [self parseUNNotification:response.notification messageType:@"NotificationResponse" openedFromTray:true]; - - [_callbackHandlers setObject:[completionHandler copy] forKey:message[@"messageId"]]; - - [RNFirebaseUtil sendJSEvent:self name:MESSAGING_MESSAGE_RECEIVED body:message]; + + [RNFirebaseUtil sendJSEvent:self name:NOTIFICATIONS_NOTIFICATION_CLICKED body:message]; + completionHandler(); } #endif