diff --git a/packages/firebase_messaging/firebase_messaging/ios/firebase_messaging/Sources/firebase_messaging/FLTFirebaseMessagingPlugin.m b/packages/firebase_messaging/firebase_messaging/ios/firebase_messaging/Sources/firebase_messaging/FLTFirebaseMessagingPlugin.m index d62ffdc0dc2f..7af15f16070f 100644 --- a/packages/firebase_messaging/firebase_messaging/ios/firebase_messaging/Sources/firebase_messaging/FLTFirebaseMessagingPlugin.m +++ b/packages/firebase_messaging/firebase_messaging/ios/firebase_messaging/Sources/firebase_messaging/FLTFirebaseMessagingPlugin.m @@ -89,6 +89,11 @@ + (void)registerWithRegistrar:(NSObject *)registrar { [registrar addMethodCallDelegate:instance channel:channel]; #if !TARGET_OS_OSX [registrar publish:instance]; // iOS only supported + if (@available(iOS 13.0, *)) { + if ([registrar respondsToSelector:@selector(addSceneDelegate:)]) { + [registrar performSelector:@selector(addSceneDelegate:) withObject:instance]; + } + } #endif } @@ -530,6 +535,30 @@ - (BOOL)application:(UIApplication *)application } // didReceiveRemoteNotification #endif +#pragma mark - SceneDelegate Methods + +#if !TARGET_OS_OSX +- (BOOL)scene:(UIScene *)scene + willConnectToSession:(UISceneSession *)session + options:(UISceneConnectionOptions *)connectionOptions { + // Handle launch notification if present + NSDictionary *remoteNotification = + connectionOptions.notificationResponse.notification.request.content.userInfo; + if (remoteNotification != nil) { + // If remoteNotification exists, it is the notification that opened the app. + _initialNotification = + [FLTFirebaseMessagingPlugin remoteMessageUserInfoToDict:remoteNotification]; + _initialNotificationID = remoteNotification[@"gcm.message_id"]; + } + + // Register for remote notifications in scene delegate + // This is critical for getting APNS token when using UISceneDelegate + [[UIApplication sharedApplication] registerForRemoteNotifications]; + + return YES; +} +#endif + #pragma mark - Firebase Messaging API - (void)messagingUnsubscribeFromTopic:(id)arguments diff --git a/packages/firebase_messaging/firebase_messaging/ios/firebase_messaging/Sources/firebase_messaging/include/FLTFirebaseMessagingPlugin.h b/packages/firebase_messaging/firebase_messaging/ios/firebase_messaging/Sources/firebase_messaging/include/FLTFirebaseMessagingPlugin.h index 2f32bac24d5d..d233a4889764 100644 --- a/packages/firebase_messaging/firebase_messaging/ios/firebase_messaging/Sources/firebase_messaging/include/FLTFirebaseMessagingPlugin.h +++ b/packages/firebase_messaging/firebase_messaging/ios/firebase_messaging/Sources/firebase_messaging/include/FLTFirebaseMessagingPlugin.h @@ -29,6 +29,11 @@ // Suppress warning - use can add the Flutter plugin for Firebase Analytics. #define FIREBASE_ANALYTICS_SUPPRESS_WARNING +// Forward declaration for FlutterSceneLifeCycleDelegate if not available +#if !TARGET_OS_OSX +@protocol FlutterSceneLifeCycleDelegate; +#endif + #if TARGET_OS_OSX #ifdef __FF_NOTIFICATIONS_SUPPORTED_PLATFORM @interface FLTFirebaseMessagingPlugin : FLTFirebasePlugin + UIApplicationDelegate +#if __has_include() || defined(FlutterSceneLifeCycleDelegate) + , + FlutterSceneLifeCycleDelegate +#endif + > #else -@interface FLTFirebaseMessagingPlugin - : FLTFirebasePlugin +@interface FLTFirebaseMessagingPlugin : FLTFirebasePlugin ) || defined(FlutterSceneLifeCycleDelegate) + , + FlutterSceneLifeCycleDelegate +#endif + > #endif #endif -@end +@end \ No newline at end of file