Skip to content

Commit a863397

Browse files
authored
feat(firebase_messaging, iOS): add scene delegate support for firebase_messaging (#17888)
* feat(firebase_messaging): implement scene delegate support for iOS * feat(firebase_messaging, iOS): add scene delegate support firebase_messaging * chore: add missing newline at end of AppDelegate.h, AppDelegate.m, and Info.plist
1 parent 345e14f commit a863397

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed

packages/firebase_messaging/firebase_messaging/ios/firebase_messaging/Sources/firebase_messaging/FLTFirebaseMessagingPlugin.m

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,11 @@ + (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar> *)registrar {
8989
[registrar addMethodCallDelegate:instance channel:channel];
9090
#if !TARGET_OS_OSX
9191
[registrar publish:instance]; // iOS only supported
92+
if (@available(iOS 13.0, *)) {
93+
if ([registrar respondsToSelector:@selector(addSceneDelegate:)]) {
94+
[registrar performSelector:@selector(addSceneDelegate:) withObject:instance];
95+
}
96+
}
9297
#endif
9398
}
9499

@@ -530,6 +535,30 @@ - (BOOL)application:(UIApplication *)application
530535
} // didReceiveRemoteNotification
531536
#endif
532537

538+
#pragma mark - SceneDelegate Methods
539+
540+
#if !TARGET_OS_OSX
541+
- (BOOL)scene:(UIScene *)scene
542+
willConnectToSession:(UISceneSession *)session
543+
options:(UISceneConnectionOptions *)connectionOptions {
544+
// Handle launch notification if present
545+
NSDictionary *remoteNotification =
546+
connectionOptions.notificationResponse.notification.request.content.userInfo;
547+
if (remoteNotification != nil) {
548+
// If remoteNotification exists, it is the notification that opened the app.
549+
_initialNotification =
550+
[FLTFirebaseMessagingPlugin remoteMessageUserInfoToDict:remoteNotification];
551+
_initialNotificationID = remoteNotification[@"gcm.message_id"];
552+
}
553+
554+
// Register for remote notifications in scene delegate
555+
// This is critical for getting APNS token when using UISceneDelegate
556+
[[UIApplication sharedApplication] registerForRemoteNotifications];
557+
558+
return YES;
559+
}
560+
#endif
561+
533562
#pragma mark - Firebase Messaging API
534563

535564
- (void)messagingUnsubscribeFromTopic:(id)arguments

packages/firebase_messaging/firebase_messaging/ios/firebase_messaging/Sources/firebase_messaging/include/FLTFirebaseMessagingPlugin.h

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
// Suppress warning - use can add the Flutter plugin for Firebase Analytics.
3030
#define FIREBASE_ANALYTICS_SUPPRESS_WARNING
3131

32+
// Forward declaration for FlutterSceneLifeCycleDelegate if not available
33+
#if !TARGET_OS_OSX
34+
@protocol FlutterSceneLifeCycleDelegate;
35+
#endif
36+
3237
#if TARGET_OS_OSX
3338
#ifdef __FF_NOTIFICATIONS_SUPPORTED_PLATFORM
3439
@interface FLTFirebaseMessagingPlugin : FLTFirebasePlugin <FlutterPlugin,
@@ -48,11 +53,21 @@ API_AVAILABLE(ios(10.0))
4853
@interface FLTFirebaseMessagingPlugin : FLTFirebasePlugin <FlutterPlugin,
4954
FLTFirebasePlugin,
5055
FIRMessagingDelegate,
51-
UIApplicationDelegate,
52-
UNUserNotificationCenterDelegate>
56+
UIApplicationDelegate
57+
#if __has_include(<Flutter/FlutterSceneLifeCycleDelegate.h>) || defined(FlutterSceneLifeCycleDelegate)
58+
,
59+
FlutterSceneLifeCycleDelegate
60+
#endif
61+
>
5362
#else
54-
@interface FLTFirebaseMessagingPlugin
55-
: FLTFirebasePlugin <FlutterPlugin, FLTFirebasePlugin, FIRMessagingDelegate>
63+
@interface FLTFirebaseMessagingPlugin : FLTFirebasePlugin <FlutterPlugin,
64+
FLTFirebasePlugin,
65+
FIRMessagingDelegate
66+
#if __has_include(<Flutter/FlutterSceneLifeCycleDelegate.h>) || defined(FlutterSceneLifeCycleDelegate)
67+
,
68+
FlutterSceneLifeCycleDelegate
69+
#endif
70+
>
5671
#endif
5772
#endif
58-
@end
73+
@end

0 commit comments

Comments
 (0)