@@ -307,11 +307,6 @@ - (void)setupNotificationListeners {
307
307
// To prevent multiple notifications remove self as observer for all events.
308
308
NSNotificationCenter *center = [NSNotificationCenter defaultCenter ];
309
309
[center removeObserver: self ];
310
-
311
- [center addObserver: self
312
- selector: @selector (didReceiveDefaultInstanceIDToken: )
313
- name: kFIRMessagingFCMTokenNotification
314
- object: nil ];
315
310
[center addObserver: self
316
311
selector: @selector (defaultInstanceIDTokenWasRefreshed: )
317
312
name: kFIRMessagingRegistrationTokenRefreshNotification
@@ -664,6 +659,10 @@ - (void)notifyDelegateOfFCMTokenAvailability {
664
659
if ([self .delegate respondsToSelector: @selector (messaging:didReceiveRegistrationToken: )]) {
665
660
[self .delegate messaging: self didReceiveRegistrationToken: self .defaultFcmToken];
666
661
}
662
+ // Should always trigger the token refresh notification when the delegate method is called
663
+ NSNotificationCenter *center = [NSNotificationCenter defaultCenter ];
664
+ [center postNotificationName: FIRMessagingRegistrationTokenRefreshedNotification
665
+ object: self .defaultFcmToken];
667
666
}
668
667
669
668
#pragma mark - Application State Changes
@@ -931,41 +930,29 @@ - (FIRMessagingNetworkStatus)networkType {
931
930
932
931
#pragma mark - Notifications
933
932
934
- - (void )didReceiveDefaultInstanceIDToken :(NSNotification *)notification {
933
+ - (void )defaultInstanceIDTokenWasRefreshed :(NSNotification *)notification {
935
934
if (notification.object && ![notification.object isKindOfClass: [NSString class ]]) {
936
935
FIRMessagingLoggerDebug (kFIRMessagingMessageCodeMessaging015 ,
937
936
@" Invalid default FCM token type %@ " ,
938
937
NSStringFromClass ([notification.object class ]));
939
938
return ;
940
939
}
940
+ // Retrieve the Instance ID default token, and should notify delegate and
941
+ // trigger notification as long as the token is different from previous state.
941
942
NSString *oldToken = self.defaultFcmToken ;
942
- self.defaultFcmToken = [(NSString *)notification.object copy ];
943
- if (self.defaultFcmToken && ![self .defaultFcmToken isEqualToString: oldToken]) {
943
+ NSString *token = [(NSString *)notification.object copy ];
944
+ self.defaultFcmToken = [token copy ];
945
+ if ((self.defaultFcmToken .length && oldToken.length &&
946
+ ![self .defaultFcmToken isEqualToString: oldToken]) ||
947
+ self.defaultFcmToken .length != oldToken.length ) {
944
948
[self notifyDelegateOfFCMTokenAvailability ];
945
- }
946
- [self .pubsub scheduleSync: YES ];
949
+ [self .pubsub scheduleSync: YES ];
947
950
#pragma clang diagnostic push
948
951
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
949
- if (self.shouldEstablishDirectChannel ) {
950
- [self updateAutomaticClientConnection ];
951
- }
952
- #pragma clang diagnostic pop
953
- }
954
-
955
- - (void )defaultInstanceIDTokenWasRefreshed:(NSNotification *)notification {
956
- // Retrieve the Instance ID default token, and if it is non-nil, post it
957
- NSString *token = self.instanceID .token ;
958
- // Sometimes Instance ID doesn't yet have a token, so wait until the default
959
- // token is fetched, and then notify. This ensures that this token should not
960
- // be nil when the developer accesses it.
961
- if (token != nil ) {
962
- NSString *oldToken = self.defaultFcmToken ;
963
- self.defaultFcmToken = [token copy ];
964
- if (self.defaultFcmToken && ![self .defaultFcmToken isEqualToString: oldToken]) {
965
- [self notifyDelegateOfFCMTokenAvailability ];
952
+ if (self.shouldEstablishDirectChannel ) {
953
+ [self updateAutomaticClientConnection ];
966
954
}
967
- NSNotificationCenter *center = [NSNotificationCenter defaultCenter ];
968
- [center postNotificationName: FIRMessagingRegistrationTokenRefreshedNotification object: nil ];
955
+ #pragma clang diagnostic pop
969
956
}
970
957
}
971
958
0 commit comments