@@ -302,6 +302,10 @@ - (void)tokenWithAuthorizedEntity:(NSString *)authorizedEntity
302
302
}
303
303
304
304
FIRInstanceIDTokenHandler newHandler = ^(NSString *token, NSError *error) {
305
+ if (!error && [self isDefaultTokenWithAuthorizedEntity: authorizedEntity scope: scope]) {
306
+ // The local cache should be updated as it is critical for sending token updates.
307
+ self.defaultFCMToken = token;
308
+ }
305
309
dispatch_async (dispatch_get_main_queue (), ^{
306
310
handler (token, error);
307
311
});
@@ -381,8 +385,7 @@ - (void)deleteTokenWithAuthorizedEntity:(NSString *)authorizedEntity
381
385
382
386
FIRInstanceIDDeleteTokenHandler newHandler = ^(NSError *error) {
383
387
// If a default token is deleted successfully, reset the defaultFCMToken too.
384
- if (!error && [authorizedEntity isEqualToString: self .fcmSenderID] &&
385
- [scope isEqualToString: kFIRInstanceIDDefaultTokenScope ]) {
388
+ if (!error && [self isDefaultTokenWithAuthorizedEntity: authorizedEntity scope: scope]) {
386
389
self.defaultFCMToken = nil ;
387
390
}
388
391
dispatch_async (dispatch_get_main_queue (), ^{
@@ -707,6 +710,7 @@ - (void)setupNotificationListeners {
707
710
name: kFIRInstanceIDAPNSTokenNotification
708
711
object: nil ];
709
712
[self observeFirebaseInstallationIDChanges ];
713
+ [self observeFirebaseMessagingTokenChanges ];
710
714
}
711
715
712
716
#pragma mark - Private Helpers
@@ -763,15 +767,8 @@ - (void)defaultTokenWithRetry:(BOOL)retry handler:(nullable FIRInstanceIDTokenHa
763
767
764
768
NSDictionary *instanceIDOptions = @{};
765
769
BOOL hasFirebaseMessaging = NSClassFromString (kFIRInstanceIDFCMSDKClassString ) != nil ;
766
- if (hasFirebaseMessaging && self.apnsTokenData ) {
767
- BOOL isSandboxApp = (self.apnsTokenType == FIRInstanceIDAPNSTokenTypeSandbox);
768
- if (self.apnsTokenType == FIRInstanceIDAPNSTokenTypeUnknown) {
769
- isSandboxApp = [self isSandboxApp ];
770
- }
771
- instanceIDOptions = @{
772
- kFIRInstanceIDTokenOptionsAPNSKey : self.apnsTokenData ,
773
- kFIRInstanceIDTokenOptionsAPNSIsSandboxKey : @(isSandboxApp),
774
- };
770
+ if (hasFirebaseMessaging) {
771
+ instanceIDOptions = [self defaultTokenOptions ];
775
772
}
776
773
777
774
FIRInstanceID_WEAKIFY (self);
@@ -871,6 +868,11 @@ - (void)retryGetDefaultTokenAfter:(NSTimeInterval)retryInterval {
871
868
});
872
869
}
873
870
871
+ - (BOOL )isDefaultTokenWithAuthorizedEntity : (NSString *)authorizedEntity scope : (NSString *)scope {
872
+ return [authorizedEntity isEqualToString: self .fcmSenderID] &&
873
+ [scope isEqualToString: kFIRInstanceIDDefaultTokenScope ];
874
+ }
875
+
874
876
#pragma mark - APNS Token
875
877
// This should only be triggered from FCM.
876
878
- (void )notifyAPNSTokenIsSet : (NSNotification *)notification {
@@ -1117,4 +1119,40 @@ - (void)observeFirebaseInstallationIDChanges {
1117
1119
object: nil ];
1118
1120
}
1119
1121
1122
+ - (void )observeFirebaseMessagingTokenChanges {
1123
+ [[NSNotificationCenter defaultCenter ]
1124
+ removeObserver: self
1125
+ name: kFIRInstanceIDMessagingUpdateTokenNotification
1126
+ object: nil ];
1127
+ [[NSNotificationCenter defaultCenter ]
1128
+ addObserver: self
1129
+ selector: @selector (messagingTokenDidChangeNotificationReceived: )
1130
+ name: kFIRInstanceIDMessagingUpdateTokenNotification
1131
+ object: nil ];
1132
+ }
1133
+
1134
+ - (void )messagingTokenDidChangeNotificationReceived : (NSNotification *)notification {
1135
+ NSString *tokenUpdatedFromMessaging = notification.object ;
1136
+ if (!tokenUpdatedFromMessaging || [tokenUpdatedFromMessaging isKindOfClass: [NSString class ]]) {
1137
+ self.defaultFCMToken = tokenUpdatedFromMessaging;
1138
+ [self .tokenManager saveDefaultToken: tokenUpdatedFromMessaging
1139
+ withOptions: [self defaultTokenOptions ]];
1140
+ }
1141
+ }
1142
+
1143
+ - (NSDictionary *)defaultTokenOptions {
1144
+ NSDictionary *tokenOptions = @{};
1145
+ if (self.apnsTokenData ) {
1146
+ BOOL isSandboxApp = (self.apnsTokenType == FIRInstanceIDAPNSTokenTypeSandbox);
1147
+ if (self.apnsTokenType == FIRInstanceIDAPNSTokenTypeUnknown) {
1148
+ isSandboxApp = [self isSandboxApp ];
1149
+ }
1150
+ tokenOptions = @{
1151
+ kFIRInstanceIDTokenOptionsAPNSKey : self.apnsTokenData ,
1152
+ kFIRInstanceIDTokenOptionsAPNSIsSandboxKey : @(isSandboxApp),
1153
+ };
1154
+ }
1155
+ return tokenOptions;
1156
+ }
1157
+
1120
1158
@end
0 commit comments