@@ -22,6 +22,8 @@ @implementation FLTFirebaseMessagingPlugin {
2222 NSObject <FlutterPluginRegistrar> *_registrar;
2323 NSData *_apnsToken;
2424 NSDictionary *_initialNotification;
25+ NSString *_initialNoticationID;
26+ NSString *_notificationOpenedAppID;
2527
2628#ifdef __FF_NOTIFICATIONS_SUPPORTED_PLATFORM
2729 API_AVAILABLE (ios (10 ), macosx (10.14 ))
@@ -43,7 +45,6 @@ - (instancetype)initWithFlutterMethodChannel:(FlutterMethodChannel *)channel
4345 if (self) {
4446 _channel = channel;
4547 _registrar = registrar;
46-
4748 // Application
4849 // Dart -> `getInitialNotification`
4950 // ObjC -> Initialize other delegates & observers
@@ -204,6 +205,7 @@ - (void)application_onDidFinishLaunchingNotification:(nonnull NSNotification *)n
204205 // If remoteNotification exists, it is the notification that opened the app.
205206 _initialNotification =
206207 [FLTFirebaseMessagingPlugin remoteMessageUserInfoToDict: remoteNotification];
208+ _initialNoticationID = remoteNotification[@" gcm.message_id" ];
207209 }
208210
209211#if TARGET_OS_OSX
@@ -334,8 +336,11 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
334336 withCompletionHandler : (void (^)(void ))completionHandler
335337 API_AVAILABLE(macos(10.14 ), ios(10.0 )) {
336338 NSDictionary *remoteNotification = response.notification .request .content .userInfo ;
337- // We only want to handle FCM notifications.
338- if (remoteNotification[@" gcm.message_id" ]) {
339+ _notificationOpenedAppID = remoteNotification[@" gcm.message_id" ];
340+ // We only want to handle FCM notifications and stop firing `onMessageOpenedApp()` when app is
341+ // coming from a terminated state.
342+ if (_notificationOpenedAppID != nil &&
343+ ![_initialNoticationID isEqualToString: _notificationOpenedAppID]) {
339344 NSDictionary *notificationDict =
340345 [FLTFirebaseMessagingPlugin remoteMessageUserInfoToDict: remoteNotification];
341346 [_channel invokeMethod: @" Messaging#onMessageOpenedApp" arguments: notificationDict];
@@ -995,7 +1000,10 @@ - (void)ensureAPNSTokenSetting {
9951000
9961001- (nullable NSDictionary *)copyInitialNotification {
9971002 @synchronized (self) {
998- if (_initialNotification != nil ) {
1003+ // Only return if initial notification was sent when app is terminated. Also ensure that
1004+ // it was the initial notification that was tapped to open the app.
1005+ if (_initialNotification != nil &&
1006+ [_initialNoticationID isEqualToString: _notificationOpenedAppID]) {
9991007 NSDictionary *initialNotificationCopy = [_initialNotification copy ];
10001008 _initialNotification = nil ;
10011009 return initialNotificationCopy;
0 commit comments