2222#import < FirebaseCore/FIRLibrary.h>
2323#import < FirebaseCore/FIROptions.h>
2424#import < GoogleUtilities/GULAppEnvironmentUtil.h>
25+ #import < GoogleUtilities/GULUserDefaults.h>
2526#import " FIRInstanceID+Private.h"
2627#import " FIRInstanceIDAuthService.h"
2728#import " FIRInstanceIDCheckinPreferences.h"
6768static NSString *const kAPSEnvironmentDevelopmentValue = @" development" ;
6869// / FIRMessaging selector that returns the current FIRMessaging auto init
6970// / enabled flag.
70- static NSString *const kFIRInstanceIDFCMSelectorAutoInitEnabled = @" isAutoInitEnabled " ;
71- static NSString * const kFIRInstanceIDFCMSelectorInstance = @" messaging " ;
71+ static NSString *const kFIRInstanceIDFCMSelectorAutoInitEnabled =
72+ @" isAutoInitEnabledWithUserDefaults: " ;
7273
7374static NSString *const kFIRInstanceIDAPNSTokenType = @" APNSTokenType" ;
7475static NSString *const kFIRIIDAppReadyToConfigureSDKNotification =
7778static NSString *const kFIRIIDErrorDomain = @" com.firebase.instanceid" ;
7879static NSString *const kFIRIIDServiceInstanceID = @" InstanceID" ;
7980
80- static NSInteger const kFIRIIDErrorCodeInstanceIDFailed = -121 ;
81-
8281typedef void (^FIRInstanceIDKeyPairHandler)(FIRInstanceIDKeyPair *keyPair, NSError *error);
8382
8483/* *
@@ -639,41 +638,41 @@ + (void)load {
639638+ (nonnull NSArray <FIRComponent *> *)componentsToRegister {
640639 FIRComponentCreationBlock creationBlock =
641640 ^id _Nullable (FIRComponentContainer *container, BOOL *isCacheable) {
641+ // InstanceID only works with the default app.
642+ if (!container.app .isDefaultApp ) {
643+ // Only configure for the default FIRApp.
644+ FIRInstanceIDLoggerDebug (kFIRInstanceIDMessageCodeFIRApp002 ,
645+ @" Firebase Instance ID only works with the default app." );
646+ return nil ;
647+ }
648+
642649 // Ensure it's cached so it returns the same instance every time instanceID is called.
643650 *isCacheable = YES ;
644651 FIRInstanceID *instanceID = [[FIRInstanceID alloc ] initPrivately ];
645652 [instanceID start ];
653+ [instanceID configureInstanceIDWithOptions: container.app.options];
646654 return instanceID;
647655 };
648656 FIRComponent *instanceIDProvider =
649657 [FIRComponent componentWithProtocol: @protocol (FIRInstanceIDInstanceProvider)
650- instantiationTiming: FIRInstantiationTimingLazy
658+ instantiationTiming: FIRInstantiationTimingEagerInDefaultApp
651659 dependencies: @[]
652660 creationBlock: creationBlock];
653661 return @[ instanceIDProvider ];
654662}
655663
656- + (void )configureWithApp : (FIRApp *)app {
657- if (!app.isDefaultApp ) {
658- // Only configure for the default FIRApp.
659- FIRInstanceIDLoggerDebug (kFIRInstanceIDMessageCodeFIRApp002 ,
660- @" Firebase Instance ID only works with the default app." );
661- return ;
662- }
663- [[FIRInstanceID instanceID ] configureInstanceIDWithOptions: app.options app: app];
664- }
665-
666- - (void )configureInstanceIDWithOptions : (FIROptions *)options app : (FIRApp *)firApp {
664+ - (void )configureInstanceIDWithOptions : (FIROptions *)options {
667665 NSString *GCMSenderID = options.GCMSenderID ;
668666 if (!GCMSenderID.length ) {
669667 FIRInstanceIDLoggerError (kFIRInstanceIDMessageCodeFIRApp000 ,
670668 @" Firebase not set up correctly, nil or empty senderID." );
671- [FIRInstanceID exitWithReason: @" GCM_SENDER_ID must not be nil or empty." forFirebaseApp: firApp];
672- return ;
669+ [NSException raise: kFIRIIDErrorDomain
670+ format: @" Could not configure Firebase InstanceID. GCMSenderID must not be nil or "
671+ @" empty." ];
673672 }
674673
675674 self.fcmSenderID = GCMSenderID;
676- self.firebaseAppID = firApp. options .googleAppID ;
675+ self.firebaseAppID = options.googleAppID ;
677676
678677 // FCM generates a FCM token during app start for sending push notification to device.
679678 // This is not needed for app extension.
@@ -682,26 +681,6 @@ - (void)configureInstanceIDWithOptions:(FIROptions *)options app:(FIRApp *)firAp
682681 }
683682}
684683
685- + (NSError *)configureErrorWithReason : (nonnull NSString *)reason {
686- NSString *description =
687- [NSString stringWithFormat: @" Configuration failed for service %@ ." , kFIRIIDServiceInstanceID ];
688- if (!reason.length ) {
689- reason = @" Unknown reason" ;
690- }
691-
692- NSDictionary *userInfo =
693- @{NSLocalizedDescriptionKey : description, NSLocalizedFailureReasonErrorKey : reason};
694-
695- return [NSError errorWithDomain: kFIRIIDErrorDomain
696- code: kFIRIIDErrorCodeInstanceIDFailed
697- userInfo: userInfo];
698- }
699-
700- + (void )exitWithReason : (nonnull NSString *)reason forFirebaseApp : (FIRApp *)firebaseApp {
701- [NSException raise: kFIRIIDErrorDomain
702- format: @" Could not configure Firebase InstanceID. %@ " , reason];
703- }
704-
705684// This is used to start any operations when we receive FirebaseSDK setup notification
706685// from FIRCore.
707686- (void )didCompleteConfigure {
@@ -738,29 +717,20 @@ - (BOOL)isFCMAutoInitEnabled {
738717 return NO ;
739718 }
740719
741- // Messaging doesn't have the singleton method, auto init should be enabled since FCM exists.
742- SEL instanceSelector = NSSelectorFromString (kFIRInstanceIDFCMSelectorInstance );
743- if (![messagingClass respondsToSelector: instanceSelector]) {
744- return YES ;
745- }
746-
747- // Get FIRMessaging shared instance.
748- IMP messagingInstanceIMP = [messagingClass methodForSelector: instanceSelector];
749- id (*getMessagingInstance)(id , SEL ) = (void *)messagingInstanceIMP;
750- id messagingInstance = getMessagingInstance (messagingClass, instanceSelector);
751-
752- // Messaging doesn't have the property, auto init should be enabled since FCM exists.
720+ // Messaging doesn't have the class method, auto init should be enabled since FCM exists.
753721 SEL autoInitSelector = NSSelectorFromString (kFIRInstanceIDFCMSelectorAutoInitEnabled );
754- if (![messagingInstance respondsToSelector: autoInitSelector]) {
722+ if (![messagingClass respondsToSelector: autoInitSelector]) {
755723 return YES ;
756724 }
757725
758- // Get autoInitEnabled method.
759- IMP isAutoInitEnabledIMP = [messagingInstance methodForSelector: autoInitSelector];
760- BOOL (*isAutoInitEnabled)(id , SEL ) = (BOOL (*)(id , SEL ))isAutoInitEnabledIMP;
726+ // Get the autoInitEnabled class method.
727+ IMP isAutoInitEnabledIMP = [messagingClass methodForSelector: autoInitSelector];
728+ BOOL (*isAutoInitEnabled)
729+ (Class , SEL , GULUserDefaults *) = (BOOL (*)(id , SEL , GULUserDefaults *))isAutoInitEnabledIMP;
761730
762731 // Check FCM's isAutoInitEnabled property.
763- return isAutoInitEnabled (messagingInstance, autoInitSelector);
732+ return isAutoInitEnabled (messagingClass, autoInitSelector,
733+ [GULUserDefaults standardUserDefaults ]);
764734}
765735
766736// Actually makes InstanceID instantiate both the IID and Token-related subsystems.
0 commit comments