Skip to content

Commit e66c59c

Browse files
fix(firebase_messaging, iOS): ensure initial notification was tapped to open app. fixes getInitialMessage() & onMessageOpenedApp() . (#9315)
Co-authored-by: Mike Diarmid <[email protected]>
1 parent a620412 commit e66c59c

File tree

2 files changed

+28
-15
lines changed

2 files changed

+28
-15
lines changed

packages/firebase_messaging/firebase_messaging/example/lib/main.dart

+16-11
Original file line numberDiff line numberDiff line change
@@ -174,17 +174,6 @@ class _Application extends State<Application> {
174174
@override
175175
void initState() {
176176
super.initState();
177-
FirebaseMessaging.instance
178-
.getInitialMessage()
179-
.then((RemoteMessage? message) {
180-
if (message != null) {
181-
Navigator.pushNamed(
182-
context,
183-
'/message',
184-
arguments: MessageArguments(message, true),
185-
);
186-
}
187-
});
188177

189178
FirebaseMessaging.onMessage.listen(showFlutterNotification);
190179

@@ -308,6 +297,22 @@ class _Application extends State<Application> {
308297
: Text(token, style: const TextStyle(fontSize: 12));
309298
}),
310299
),
300+
ElevatedButton(
301+
onPressed: () {
302+
FirebaseMessaging.instance
303+
.getInitialMessage()
304+
.then((RemoteMessage? message) {
305+
if (message != null) {
306+
Navigator.pushNamed(
307+
context,
308+
'/message',
309+
arguments: MessageArguments(message, true),
310+
);
311+
}
312+
});
313+
},
314+
child: const Text('getInitialMessage()'),
315+
),
311316
MetaCard('Message Stream', MessageList()),
312317
],
313318
),

packages/firebase_messaging/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m

+12-4
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)