Skip to content

Commit

Permalink
fix(firebase_messaging, iOS): ensure initial notification was tapped …
Browse files Browse the repository at this point in the history
…to open app. fixes `getInitialMessage()` & `onMessageOpenedApp()` . (#9315)

Co-authored-by: Mike Diarmid <mike.diarmid@gmail.com>
  • Loading branch information
russellwheatley and Salakar committed Aug 10, 2022
1 parent a620412 commit e66c59c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
Expand Up @@ -174,17 +174,6 @@ class _Application extends State<Application> {
@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(showFlutterNotification);

Expand Down Expand Up @@ -308,6 +297,22 @@ class _Application extends State<Application> {
: 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()),
],
),
Expand Down
Expand Up @@ -22,6 +22,8 @@ @implementation FLTFirebaseMessagingPlugin {
NSObject<FlutterPluginRegistrar> *_registrar;
NSData *_apnsToken;
NSDictionary *_initialNotification;
NSString *_initialNoticationID;
NSString *_notificationOpenedAppID;

#ifdef __FF_NOTIFICATIONS_SUPPORTED_PLATFORM
API_AVAILABLE(ios(10), macosx(10.14))
Expand All @@ -43,7 +45,6 @@ - (instancetype)initWithFlutterMethodChannel:(FlutterMethodChannel *)channel
if (self) {
_channel = channel;
_registrar = registrar;

// Application
// Dart -> `getInitialNotification`
// ObjC -> Initialize other delegates & observers
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e66c59c

Please sign in to comment.