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.