Skip to content

Commit 6f22596

Browse files
authored
fix(messaging,ios): fix an issue where FirebaseMessaging could init even if FirebaseMessagingAutoInitEnabled was disabled (#18452)
1 parent 3cefea2 commit 6f22596

1 file changed

Lines changed: 28 additions & 14 deletions

File tree

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

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,16 @@ - (void)setupNotificationHandlingWithRemoteNotification:
240240
[self setupNotificationHandlingWithRemoteNotification:remoteNotification actionIdentifier:nil];
241241
}
242242

243+
- (void)registerForRemoteNotifications {
244+
#if TARGET_OS_OSX
245+
if (@available(macOS 10.14, *)) {
246+
[[NSApplication sharedApplication] registerForRemoteNotifications];
247+
}
248+
#else
249+
[[UIApplication sharedApplication] registerForRemoteNotifications];
250+
#endif
251+
}
252+
243253
- (void)setupNotificationHandlingWithRemoteNotification:(nullable NSDictionary *)remoteNotification
244254
actionIdentifier:(nullable NSString *)actionIdentifier {
245255
// If notification handling was already set up (e.g. from
@@ -370,14 +380,11 @@ - (void)setupNotificationHandlingWithRemoteNotification:(nullable NSDictionary *
370380
// We automatically register for remote notifications as
371381
// application:didReceiveRemoteNotification:fetchCompletionHandler: will not get called unless
372382
// registerForRemoteNotifications is called early on during app initialization, calling this from
373-
// Dart would be too late.
374-
#if TARGET_OS_OSX
375-
if (@available(macOS 10.14, *)) {
376-
[[NSApplication sharedApplication] registerForRemoteNotifications];
383+
// Dart would be too late. Defer registration when auto-init is disabled so the APNs token cannot
384+
// trigger FCM registration before the user opts in.
385+
if ([FIRMessaging messaging].isAutoInitEnabled) {
386+
[self registerForRemoteNotifications];
377387
}
378-
#else
379-
[[UIApplication sharedApplication] registerForRemoteNotifications];
380-
#endif
381388
}
382389

383390
- (void)markInitialNotificationGatheredAfterDelay {
@@ -546,13 +553,15 @@ - (void)application:(NSApplication *)application
546553
- (void)application:(UIApplication *)application
547554
didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
548555
#endif
549-
if ([FIRMessaging messaging] == nil) {
556+
FIRMessaging *messaging = [FIRMessaging messaging];
557+
if (!messaging.isAutoInitEnabled) {
550558
_apnsToken = deviceToken;
559+
return;
551560
}
552561
#ifdef DEBUG
553-
[[FIRMessaging messaging] setAPNSToken:deviceToken type:FIRMessagingAPNSTokenTypeSandbox];
562+
[messaging setAPNSToken:deviceToken type:FIRMessagingAPNSTokenTypeSandbox];
554563
#else
555-
[[FIRMessaging messaging] setAPNSToken:deviceToken type:FIRMessagingAPNSTokenTypeProd];
564+
[messaging setAPNSToken:deviceToken type:FIRMessagingAPNSTokenTypeProd];
556565
#endif
557566
}
558567

@@ -729,7 +738,12 @@ - (void)messagingSubscribeToTopic:(id)arguments
729738
- (void)messagingSetAutoInitEnabled:(id)arguments
730739
withMethodCallResult:(FLTFirebaseMethodCallResult *)result {
731740
FIRMessaging *messaging = [FIRMessaging messaging];
732-
messaging.autoInitEnabled = [arguments[@"enabled"] boolValue];
741+
BOOL enabled = [arguments[@"enabled"] boolValue];
742+
messaging.autoInitEnabled = enabled;
743+
if (enabled) {
744+
[self registerForRemoteNotifications];
745+
[self ensureAPNSTokenSetting];
746+
}
733747
result.success(@{
734748
@"isAutoInitEnabled" : @(messaging.isAutoInitEnabled),
735749
});
@@ -1207,11 +1221,11 @@ + (NSDictionary *)remoteMessageUserInfoToDict:(NSDictionary *)userInfo
12071221
- (void)ensureAPNSTokenSetting {
12081222
FIRMessaging *messaging = [FIRMessaging messaging];
12091223

1210-
if (messaging.APNSToken == nil && _apnsToken != nil) {
1224+
if (messaging.isAutoInitEnabled && messaging.APNSToken == nil && _apnsToken != nil) {
12111225
#ifdef DEBUG
1212-
[[FIRMessaging messaging] setAPNSToken:_apnsToken type:FIRMessagingAPNSTokenTypeSandbox];
1226+
[messaging setAPNSToken:_apnsToken type:FIRMessagingAPNSTokenTypeSandbox];
12131227
#else
1214-
[[FIRMessaging messaging] setAPNSToken:_apnsToken type:FIRMessagingAPNSTokenTypeProd];
1228+
[messaging setAPNSToken:_apnsToken type:FIRMessagingAPNSTokenTypeProd];
12151229
#endif
12161230
_apnsToken = nil;
12171231
}

0 commit comments

Comments
 (0)