Skip to content

fix(firebase_messaging, iOS): ensure initial notification was tapped to open app. fixes getInitialMessage() & onMessageOpenedApp() . #9315

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -120,17 +120,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((RemoteMessage message) {
RemoteNotification? notification = message.notification;
Expand Down Expand Up @@ -274,6 +263,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
Original file line number Diff line number Diff line change
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