@@ -22,6 +22,8 @@ @implementation FLTFirebaseMessagingPlugin {
22
22
NSObject <FlutterPluginRegistrar> *_registrar;
23
23
NSData *_apnsToken;
24
24
NSDictionary *_initialNotification;
25
+ NSString *_initialNoticationID;
26
+ NSString *_notificationOpenedAppID;
25
27
26
28
#ifdef __FF_NOTIFICATIONS_SUPPORTED_PLATFORM
27
29
API_AVAILABLE (ios (10 ), macosx (10.14 ))
@@ -43,7 +45,6 @@ - (instancetype)initWithFlutterMethodChannel:(FlutterMethodChannel *)channel
43
45
if (self) {
44
46
_channel = channel;
45
47
_registrar = registrar;
46
-
47
48
// Application
48
49
// Dart -> `getInitialNotification`
49
50
// ObjC -> Initialize other delegates & observers
@@ -204,6 +205,7 @@ - (void)application_onDidFinishLaunchingNotification:(nonnull NSNotification *)n
204
205
// If remoteNotification exists, it is the notification that opened the app.
205
206
_initialNotification =
206
207
[FLTFirebaseMessagingPlugin remoteMessageUserInfoToDict: remoteNotification];
208
+ _initialNoticationID = remoteNotification[@" gcm.message_id" ];
207
209
}
208
210
209
211
#if TARGET_OS_OSX
@@ -334,8 +336,11 @@ - (void)userNotificationCenter:(UNUserNotificationCenter *)center
334
336
withCompletionHandler : (void (^)(void ))completionHandler
335
337
API_AVAILABLE(macos(10.14 ), ios(10.0 )) {
336
338
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]) {
339
344
NSDictionary *notificationDict =
340
345
[FLTFirebaseMessagingPlugin remoteMessageUserInfoToDict: remoteNotification];
341
346
[_channel invokeMethod: @" Messaging#onMessageOpenedApp" arguments: notificationDict];
@@ -995,7 +1000,10 @@ - (void)ensureAPNSTokenSetting {
995
1000
996
1001
- (nullable NSDictionary *)copyInitialNotification {
997
1002
@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]) {
999
1007
NSDictionary *initialNotificationCopy = [_initialNotification copy ];
1000
1008
_initialNotification = nil ;
1001
1009
return initialNotificationCopy;
0 commit comments