1919#error FIRMessagingLib should be compiled with ARC.
2020#endif
2121
22- #import " GoogleUtilities/AppDelegateSwizzler/Public/GoogleUtilities/GULAppDelegateSwizzler.h"
23- #import " GoogleUtilities/Environment/Public/GoogleUtilities/GULAppEnvironmentUtil.h"
24- #import " GoogleUtilities/Reachability/Public/GoogleUtilities/GULReachabilityChecker.h"
25- #import " GoogleUtilities/UserDefaults/Public/GoogleUtilities/GULUserDefaults.h"
2622#import " FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessaging.h"
2723#import " Firebase/InstanceID/Private/FIRInstanceID_Private.h"
28- #import " FirebaseInstallations/Source/Library/Public/FirebaseInstallations/FirebaseInstallations.h"
2924#import " FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
25+ #import " FirebaseInstallations/Source/Library/Public/FirebaseInstallations/FirebaseInstallations.h"
3026#import " FirebaseMessaging/Sources/Interop/FIRMessagingInterop.h"
3127#import " FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessagingExtensionHelper.h"
28+ #import " GoogleUtilities/AppDelegateSwizzler/Public/GoogleUtilities/GULAppDelegateSwizzler.h"
29+ #import " GoogleUtilities/Environment/Public/GoogleUtilities/GULAppEnvironmentUtil.h"
30+ #import " GoogleUtilities/Reachability/Public/GoogleUtilities/GULReachabilityChecker.h"
31+ #import " GoogleUtilities/UserDefaults/Public/GoogleUtilities/GULUserDefaults.h"
3232#import " Interop/Analytics/Public/FIRAnalyticsInterop.h"
3333
3434#import " FirebaseMessaging/Sources/FIRMessagingAnalytics.h"
35+ #import " FirebaseMessaging/Sources/FIRMessagingCode.h"
3536#import " FirebaseMessaging/Sources/FIRMessagingConstants.h"
3637#import " FirebaseMessaging/Sources/FIRMessagingContextManagerService.h"
37- #import " FirebaseMessaging/Sources/FIRMessagingCode.h"
3838#import " FirebaseMessaging/Sources/FIRMessagingDefines.h"
3939#import " FirebaseMessaging/Sources/FIRMessagingLogger.h"
4040#import " FirebaseMessaging/Sources/FIRMessagingPubSub.h"
@@ -106,7 +106,6 @@ @interface FIRMessaging () <GULReachabilityDelegate>
106106
107107// FIRApp properties
108108@property (nonatomic , readwrite , strong ) NSData *apnsTokenData;
109- @property (nonatomic , readwrite , strong ) NSString *defaultFcmToken;
110109@property (nonatomic , readwrite , strong ) FIRMessagingClient *client;
111110@property (nonatomic , readwrite , strong ) GULReachabilityChecker *reachability;
112111@property (nonatomic , readwrite , strong ) FIRMessagingPubSub *pubsub;
@@ -247,6 +246,11 @@ - (void)didCompleteConfigure {
247246 completion: ^(NSString *_Nullable FCMToken, NSError *_Nullable error){
248247 }];
249248 }
249+ // Set the default FCM token, there's an issue that FIRApp configure
250+ // happens before developers able to set the delegate
251+ // Hence first token set must be happen here after listener is set
252+ // TODO(chliangGoogle) Need to investigate better solution.
253+ [self updateDefaultFCMToken: self .FCMToken];
250254 }];
251255 } else if (self.isAutoInitEnabled ) {
252256 // When there is no cached token, must check auto init is enabled.
@@ -315,7 +319,7 @@ - (void)setupNotificationListeners {
315319 NSNotificationCenter *center = [NSNotificationCenter defaultCenter ];
316320 [center removeObserver: self ];
317321 [center addObserver: self
318- selector: @selector (defaultInstanceIDTokenWasRefreshed : )
322+ selector: @selector (defaultFCMTokenWasRefreshed : )
319323 name: kFIRMessagingRegistrationTokenRefreshNotification
320324 object: nil ];
321325}
@@ -326,7 +330,7 @@ - (void)setupRmqManager {
326330}
327331
328332- (void )setupTopics {
329- self.pubsub = [[FIRMessagingPubSub alloc ] initWithTokenManager: _tokenManager ];
333+ self.pubsub = [[FIRMessagingPubSub alloc ] initWithTokenManager: self .tokenManager ];
330334}
331335
332336- (void )setupSyncMessageManager {
@@ -540,7 +544,8 @@ - (void)setAutoInitEnabled:(BOOL)autoInitEnabled {
540544
541545- (NSString *)FCMToken {
542546 // Gets the current default token, and requets a new one if it doesn't exist.
543- return [[FIRMessaging messaging ].tokenManager tokenAndRequestIfNotExist ];
547+ NSString *token = [self .tokenManager tokenAndRequestIfNotExist ];
548+ return token;
544549}
545550
546551- (void )tokenWithCompletion:(FIRMessagingFCMTokenFetchCompletion)completion {
@@ -625,7 +630,7 @@ - (void)deleteFCMTokenForSenderID:(nonnull NSString *)senderID
625630
626631- (void )deleteDataWithCompletion:(void (^)(NSError *_Nullable))completion {
627632 FIRMessaging_WEAKIFY (self);
628- [self .tokenManager deleteWithHandler: ^(NSError * error) {
633+ [self .tokenManager deleteWithHandler: ^(NSError *error) {
629634 FIRMessaging_STRONGIFY (self);
630635 if (error) {
631636 if (completion) {
@@ -637,8 +642,7 @@ - (void)deleteDataWithCompletion:(void (^)(NSError *_Nullable))completion {
637642 // enabled.
638643 if ([self isAutoInitEnabled ]) {
639644 // Deletion succeeds! Requesting new checkin, IID and token.
640- [self tokenWithCompletion: ^(NSString *_Nullable token,
641- NSError *_Nullable error) {
645+ [self tokenWithCompletion: ^(NSString *_Nullable token, NSError *_Nullable error) {
642646 if (completion) {
643647 completion (error);
644648 }
@@ -829,20 +833,23 @@ - (FIRMessagingNetworkStatus)networkType {
829833
830834#pragma mark - Notifications
831835
832- - (void )defaultInstanceIDTokenWasRefreshed :(NSNotification *)notification {
836+ - (void )defaultFCMTokenWasRefreshed :(NSNotification *)notification {
833837 if (notification.object && ![notification.object isKindOfClass: [NSString class ]]) {
834838 FIRMessagingLoggerDebug (kFIRMessagingMessageCodeMessaging015 ,
835839 @" Invalid default FCM token type %@ " ,
836840 NSStringFromClass ([notification.object class ]));
837841 return ;
838842 }
839- // Retrieve the Instance ID default token, and should notify delegate and
840- // trigger notification as long as the token is different from previous state.
841- NSString *oldToken = self.defaultFcmToken ;
842- self.defaultFcmToken = [(NSString *)notification.object copy ];
843- if ((self.defaultFcmToken .length && oldToken.length &&
844- ![self .defaultFcmToken isEqualToString: oldToken]) ||
845- self.defaultFcmToken .length != oldToken.length ) {
843+ NSString *newToken = [(NSString *)notification.object copy ];
844+ [self updateDefaultFCMToken: newToken];
845+ }
846+
847+ - (void )updateDefaultFCMToken:(NSString *)defaultFCMToken {
848+ NSString *oldToken = self.tokenManager .defaultFCMToken ;
849+ NSString *newToken = defaultFCMToken;
850+ if ([self .tokenManager hasTokenChangedFromOldToken: oldToken toNewToken: newToken]) {
851+ // Make sure to set default token first before notifying others.
852+ [self .tokenManager saveDefaultTokenInfoInKeychain: newToken];
846853 [self notifyDelegateOfFCMTokenAvailability ];
847854 [self .pubsub scheduleSync: YES ];
848855 }
@@ -857,12 +864,12 @@ - (void)notifyDelegateOfFCMTokenAvailability {
857864 return ;
858865 }
859866 if ([self .delegate respondsToSelector: @selector (messaging:didReceiveRegistrationToken: )]) {
860- [self .delegate messaging: self didReceiveRegistrationToken: self .defaultFcmToken ];
867+ [self .delegate messaging: self didReceiveRegistrationToken: self .tokenManager.defaultFCMToken ];
861868 }
862869 // Should always trigger the token refresh notification when the delegate method is called
863870 NSNotificationCenter *center = [NSNotificationCenter defaultCenter ];
864871 [center postNotificationName: FIRMessagingRegistrationTokenRefreshedNotification
865- object: self .defaultFcmToken ];
872+ object: self .tokenManager.defaultFCMToken ];
866873}
867874
868875#pragma mark - Application Support Directory
0 commit comments