diff --git a/packages/firebase_messaging/firebase_messaging/ios/firebase_messaging/Sources/firebase_messaging/FLTFirebaseMessagingPlugin.m b/packages/firebase_messaging/firebase_messaging/ios/firebase_messaging/Sources/firebase_messaging/FLTFirebaseMessagingPlugin.m index 77f2078991c6..40e9e3e4574f 100644 --- a/packages/firebase_messaging/firebase_messaging/ios/firebase_messaging/Sources/firebase_messaging/FLTFirebaseMessagingPlugin.m +++ b/packages/firebase_messaging/firebase_messaging/ios/firebase_messaging/Sources/firebase_messaging/FLTFirebaseMessagingPlugin.m @@ -46,6 +46,11 @@ @implementation FLTFirebaseMessagingPlugin { // Guard against calling setupNotificationHandling twice BOOL _notificationHandlingSetup; +#if TARGET_OS_OSX + // Tracks when plugin registration occurred after the macOS launch notification. + BOOL _missedApplicationDidFinishLaunchingNotification; +#endif + #ifdef __FF_NOTIFICATIONS_SUPPORTED_PLATFORM API_AVAILABLE(ios(10), macosx(10.14)) __weak id _originalNotificationCenterDelegate; @@ -89,13 +94,19 @@ + (void)registerWithRegistrar:(NSObject *)registrar { FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:kFLTFirebaseMessagingChannelName binaryMessenger:[registrar messenger]]; - id instance = [[FLTFirebaseMessagingPlugin alloc] initWithFlutterMethodChannel:channel - andFlutterPluginRegistrar:registrar]; + FLTFirebaseMessagingPlugin *instance = + [[FLTFirebaseMessagingPlugin alloc] initWithFlutterMethodChannel:channel + andFlutterPluginRegistrar:registrar]; // Register with internal FlutterFire plugin registry. [[FLTFirebasePluginRegistry sharedInstance] registerFirebasePlugin:instance]; [registrar addMethodCallDelegate:instance channel:channel]; -#if !TARGET_OS_OSX +#if TARGET_OS_OSX + if ([[NSApplication sharedApplication] isRunning]) { + instance->_missedApplicationDidFinishLaunchingNotification = YES; + [instance setupNotificationHandlingWithRemoteNotification:nil]; + } +#else [registrar publish:instance]; // iOS only supported if (@available(iOS 13.0, *)) { if ([registrar respondsToSelector:@selector(addSceneDelegate:)]) { @@ -382,7 +393,13 @@ - (void)markInitialNotificationGatheredAfterDelay { - (void)application_onDidFinishLaunchingNotification:(nonnull NSNotification *)notification { // Setup UIApplicationDelegate. #if TARGET_OS_OSX - NSDictionary *remoteNotification = notification.userInfo[NSApplicationLaunchUserNotificationKey]; + id launchNotification = notification.userInfo[NSApplicationLaunchUserNotificationKey]; + NSDictionary *remoteNotification = nil; + if ([launchNotification isKindOfClass:[NSDictionary class]]) { + remoteNotification = launchNotification; + } else if ([launchNotification respondsToSelector:@selector(userInfo)]) { + remoteNotification = [launchNotification userInfo]; + } #else NSDictionary *remoteNotification = notification.userInfo[UIApplicationLaunchOptionsRemoteNotificationKey]; @@ -469,6 +486,14 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center [FLTFirebaseMessagingPlugin remoteMessageUserInfoToDict:remoteNotification withActionIdentifier:response.actionIdentifier]; +#if TARGET_OS_OSX + if (_missedApplicationDidFinishLaunchingNotification && _initialNotification == nil) { + _initialNotification = notificationDict; + _initialNotificationID = _notificationOpenedAppID; + _initialNotificationGathered = YES; + } +#endif + if (_initialNotification != nil && [_initialNotificationID isEqualToString:_notificationOpenedAppID]) { _initialNotification = notificationDict; @@ -1194,6 +1219,9 @@ - (void)ensureAPNSTokenSetting { - (nullable NSDictionary *)copyInitialNotification { @synchronized(self) { +#if TARGET_OS_OSX + _missedApplicationDidFinishLaunchingNotification = NO; +#endif // Only return if initial notification was sent when app is terminated. Also ensure that // it was the initial notification that was tapped to open the app. if (_initialNotification != nil && diff --git a/tests/integration_test/firebase_messaging/firebase_messaging_e2e_test.dart b/tests/integration_test/firebase_messaging/firebase_messaging_e2e_test.dart index bf94039983fc..2e5175611b79 100644 --- a/tests/integration_test/firebase_messaging/firebase_messaging_e2e_test.dart +++ b/tests/integration_test/firebase_messaging/firebase_messaging_e2e_test.dart @@ -127,7 +127,12 @@ void main() { group('getInitialMessage', () { test('returns null when no initial message', () async { - expect(await messaging.getInitialMessage(), null); + expect( + await messaging + .getInitialMessage() + .timeout(const Duration(seconds: 5)), + null, + ); }); });