22
22
#import < FirebaseCore/FIRLibrary.h>
23
23
#import < FirebaseCore/FIROptions.h>
24
24
#import < GoogleUtilities/GULAppEnvironmentUtil.h>
25
+ #import < GoogleUtilities/GULUserDefaults.h>
25
26
#import " FIRInstanceID+Private.h"
26
27
#import " FIRInstanceIDAuthService.h"
27
28
#import " FIRInstanceIDCheckinPreferences.h"
67
68
static NSString *const kAPSEnvironmentDevelopmentValue = @" development" ;
68
69
// / FIRMessaging selector that returns the current FIRMessaging auto init
69
70
// / enabled flag.
70
- static NSString *const kFIRInstanceIDFCMSelectorAutoInitEnabled = @" isAutoInitEnabled " ;
71
- static NSString * const kFIRInstanceIDFCMSelectorInstance = @" messaging " ;
71
+ static NSString *const kFIRInstanceIDFCMSelectorAutoInitEnabled =
72
+ @" isAutoInitEnabledWithUserDefaults: " ;
72
73
73
74
static NSString *const kFIRInstanceIDAPNSTokenType = @" APNSTokenType" ;
74
75
static NSString *const kFIRIIDAppReadyToConfigureSDKNotification =
77
78
static NSString *const kFIRIIDErrorDomain = @" com.firebase.instanceid" ;
78
79
static NSString *const kFIRIIDServiceInstanceID = @" InstanceID" ;
79
80
80
- static NSInteger const kFIRIIDErrorCodeInstanceIDFailed = -121 ;
81
-
82
81
typedef void (^FIRInstanceIDKeyPairHandler)(FIRInstanceIDKeyPair *keyPair, NSError *error);
83
82
84
83
/* *
@@ -639,41 +638,41 @@ + (void)load {
639
638
+ (nonnull NSArray <FIRComponent *> *)componentsToRegister {
640
639
FIRComponentCreationBlock creationBlock =
641
640
^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
+
642
649
// Ensure it's cached so it returns the same instance every time instanceID is called.
643
650
*isCacheable = YES ;
644
651
FIRInstanceID *instanceID = [[FIRInstanceID alloc ] initPrivately ];
645
652
[instanceID start ];
653
+ [instanceID configureInstanceIDWithOptions: container.app.options];
646
654
return instanceID;
647
655
};
648
656
FIRComponent *instanceIDProvider =
649
657
[FIRComponent componentWithProtocol: @protocol (FIRInstanceIDInstanceProvider)
650
- instantiationTiming: FIRInstantiationTimingLazy
658
+ instantiationTiming: FIRInstantiationTimingEagerInDefaultApp
651
659
dependencies: @[]
652
660
creationBlock: creationBlock];
653
661
return @[ instanceIDProvider ];
654
662
}
655
663
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 {
667
665
NSString *GCMSenderID = options.GCMSenderID ;
668
666
if (!GCMSenderID.length ) {
669
667
FIRInstanceIDLoggerError (kFIRInstanceIDMessageCodeFIRApp000 ,
670
668
@" 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." ];
673
672
}
674
673
675
674
self.fcmSenderID = GCMSenderID;
676
- self.firebaseAppID = firApp. options .googleAppID ;
675
+ self.firebaseAppID = options.googleAppID ;
677
676
678
677
// FCM generates a FCM token during app start for sending push notification to device.
679
678
// This is not needed for app extension.
@@ -682,26 +681,6 @@ - (void)configureInstanceIDWithOptions:(FIROptions *)options app:(FIRApp *)firAp
682
681
}
683
682
}
684
683
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
-
705
684
// This is used to start any operations when we receive FirebaseSDK setup notification
706
685
// from FIRCore.
707
686
- (void )didCompleteConfigure {
@@ -738,29 +717,20 @@ - (BOOL)isFCMAutoInitEnabled {
738
717
return NO ;
739
718
}
740
719
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.
753
721
SEL autoInitSelector = NSSelectorFromString (kFIRInstanceIDFCMSelectorAutoInitEnabled );
754
- if (![messagingInstance respondsToSelector: autoInitSelector]) {
722
+ if (![messagingClass respondsToSelector: autoInitSelector]) {
755
723
return YES ;
756
724
}
757
725
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;
761
730
762
731
// Check FCM's isAutoInitEnabled property.
763
- return isAutoInitEnabled (messagingInstance, autoInitSelector);
732
+ return isAutoInitEnabled (messagingClass, autoInitSelector,
733
+ [GULUserDefaults standardUserDefaults ]);
764
734
}
765
735
766
736
// Actually makes InstanceID instantiate both the IID and Token-related subsystems.
0 commit comments