diff --git a/packages/firebase_messaging/firebase_messaging/example/lib/main.dart b/packages/firebase_messaging/firebase_messaging/example/lib/main.dart index e8b7b5b768fb..ce000787f92f 100644 --- a/packages/firebase_messaging/firebase_messaging/example/lib/main.dart +++ b/packages/firebase_messaging/firebase_messaging/example/lib/main.dart @@ -120,17 +120,6 @@ class _Application extends State { @override void initState() { super.initState(); - FirebaseMessaging.instance - .getInitialMessage() - .then((RemoteMessage? message) { - if (message != null) { - Navigator.pushNamed( - context, - '/message', - arguments: MessageArguments(message, true), - ); - } - }); FirebaseMessaging.onMessage.listen((RemoteMessage message) { RemoteNotification? notification = message.notification; @@ -274,6 +263,22 @@ class _Application extends State { : Text(token, style: const TextStyle(fontSize: 12)); }), ), + ElevatedButton( + onPressed: () { + FirebaseMessaging.instance + .getInitialMessage() + .then((RemoteMessage? message) { + if (message != null) { + Navigator.pushNamed( + context, + '/message', + arguments: MessageArguments(message, true), + ); + } + }); + }, + child: const Text('getInitialMessage()'), + ), MetaCard('Message Stream', MessageList()), ], ), diff --git a/packages/firebase_messaging/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m b/packages/firebase_messaging/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m index 334827f230e0..2dc64896f2eb 100644 --- a/packages/firebase_messaging/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m +++ b/packages/firebase_messaging/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m @@ -22,6 +22,8 @@ @implementation FLTFirebaseMessagingPlugin { NSObject *_registrar; NSData *_apnsToken; NSDictionary *_initialNotification; + NSString *_initialNoticationID; + NSString *_notificationOpenedAppID; #ifdef __FF_NOTIFICATIONS_SUPPORTED_PLATFORM API_AVAILABLE(ios(10), macosx(10.14)) @@ -43,7 +45,6 @@ - (instancetype)initWithFlutterMethodChannel:(FlutterMethodChannel *)channel if (self) { _channel = channel; _registrar = registrar; - // Application // Dart -> `getInitialNotification` // ObjC -> Initialize other delegates & observers @@ -204,6 +205,7 @@ - (void)application_onDidFinishLaunchingNotification:(nonnull NSNotification *)n // If remoteNotification exists, it is the notification that opened the app. _initialNotification = [FLTFirebaseMessagingPlugin remoteMessageUserInfoToDict:remoteNotification]; + _initialNoticationID = remoteNotification[@"gcm.message_id"]; } #if TARGET_OS_OSX @@ -334,8 +336,11 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center withCompletionHandler:(void (^)(void))completionHandler API_AVAILABLE(macos(10.14), ios(10.0)) { NSDictionary *remoteNotification = response.notification.request.content.userInfo; - // We only want to handle FCM notifications. - if (remoteNotification[@"gcm.message_id"]) { + _notificationOpenedAppID = remoteNotification[@"gcm.message_id"]; + // We only want to handle FCM notifications and stop firing `onMessageOpenedApp()` when app is + // coming from a terminated state. + if (_notificationOpenedAppID != nil && + ![_initialNoticationID isEqualToString:_notificationOpenedAppID]) { NSDictionary *notificationDict = [FLTFirebaseMessagingPlugin remoteMessageUserInfoToDict:remoteNotification]; [_channel invokeMethod:@"Messaging#onMessageOpenedApp" arguments:notificationDict]; @@ -995,7 +1000,10 @@ - (void)ensureAPNSTokenSetting { - (nullable NSDictionary *)copyInitialNotification { @synchronized(self) { - if (_initialNotification != nil) { + // 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 && + [_initialNoticationID isEqualToString:_notificationOpenedAppID]) { NSDictionary *initialNotificationCopy = [_initialNotification copy]; _initialNotification = nil; return initialNotificationCopy;