From ae0197f6dbb5a84ce004080953b5ab4d4e485b53 Mon Sep 17 00:00:00 2001 From: Russell Wheatley Date: Thu, 7 Nov 2024 13:48:20 +0000 Subject: [PATCH] fix(messaging, ios): foreground notification appears twice on iOS 18 (#13572) --- .../ios/Classes/FLTFirebaseMessagingPlugin.m | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/packages/firebase_messaging/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m b/packages/firebase_messaging/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m index 00c7400a3680..859b70d84afc 100644 --- a/packages/firebase_messaging/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m +++ b/packages/firebase_messaging/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m @@ -34,6 +34,7 @@ @implementation FLTFirebaseMessagingPlugin { NSString *_initialNotificationID; NSString *_notificationOpenedAppID; + NSString *_foregroundUniqueIdentifier; #ifdef __FF_NOTIFICATIONS_SUPPORTED_PLATFORM API_AVAILABLE(ios(10), macosx(10.14)) @@ -314,10 +315,17 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center (void (^)(UNNotificationPresentationOptions options))completionHandler API_AVAILABLE(macos(10.14), ios(10.0)) { // We only want to handle FCM notifications. - if (notification.request.content.userInfo[@"gcm.message_id"]) { + + // FIX - bug on iOS 18 which results in duplicate foreground notifications posted + // See this Apple issue: https://forums.developer.apple.com/forums/thread/761597 + // when it has been resolved, "_foregroundUniqueIdentifier" can be removed (i.e. the commit for + // this fix) + NSString *notificationIdentifier = notification.request.identifier; + + if (notification.request.content.userInfo[@"gcm.message_id"] && + ![notificationIdentifier isEqualToString:_foregroundUniqueIdentifier]) { NSDictionary *notificationDict = [FLTFirebaseMessagingPlugin NSDictionaryFromUNNotification:notification]; - [_channel invokeMethod:@"Messaging#onMessage" arguments:notificationDict]; } @@ -344,6 +352,7 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center } completionHandler(presentationOptions); } + _foregroundUniqueIdentifier = notificationIdentifier; } // Called when a user interacts with a notification.