|
|
@@ -302,6 +302,10 @@ static FIRInstanceID *gInstanceID;
|
|
|
}
|
|
|
|
|
|
FIRInstanceIDTokenHandler newHandler = ^(NSString *token, NSError *error) {
|
|
|
+ if (!error && [self isDefaultTokenWithAuthorizedEntity:authorizedEntity scope:scope]) {
|
|
|
+ // The local cache should be updated as it is critical for sending token updates.
|
|
|
+ self.defaultFCMToken = token;
|
|
|
+ }
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
handler(token, error);
|
|
|
});
|
|
|
@@ -381,8 +385,7 @@ static FIRInstanceID *gInstanceID;
|
|
|
|
|
|
FIRInstanceIDDeleteTokenHandler newHandler = ^(NSError *error) {
|
|
|
// If a default token is deleted successfully, reset the defaultFCMToken too.
|
|
|
- if (!error && [authorizedEntity isEqualToString:self.fcmSenderID] &&
|
|
|
- [scope isEqualToString:kFIRInstanceIDDefaultTokenScope]) {
|
|
|
+ if (!error && [self isDefaultTokenWithAuthorizedEntity:authorizedEntity scope:scope]) {
|
|
|
self.defaultFCMToken = nil;
|
|
|
}
|
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
@@ -707,6 +710,7 @@ static FIRInstanceID *gInstanceID;
|
|
|
name:kFIRInstanceIDAPNSTokenNotification
|
|
|
object:nil];
|
|
|
[self observeFirebaseInstallationIDChanges];
|
|
|
+ [self observeFirebaseMessagingTokenChanges];
|
|
|
}
|
|
|
|
|
|
#pragma mark - Private Helpers
|
|
|
@@ -763,15 +767,8 @@ static FIRInstanceID *gInstanceID;
|
|
|
|
|
|
NSDictionary *instanceIDOptions = @{};
|
|
|
BOOL hasFirebaseMessaging = NSClassFromString(kFIRInstanceIDFCMSDKClassString) != nil;
|
|
|
- if (hasFirebaseMessaging && self.apnsTokenData) {
|
|
|
- BOOL isSandboxApp = (self.apnsTokenType == FIRInstanceIDAPNSTokenTypeSandbox);
|
|
|
- if (self.apnsTokenType == FIRInstanceIDAPNSTokenTypeUnknown) {
|
|
|
- isSandboxApp = [self isSandboxApp];
|
|
|
- }
|
|
|
- instanceIDOptions = @{
|
|
|
- kFIRInstanceIDTokenOptionsAPNSKey : self.apnsTokenData,
|
|
|
- kFIRInstanceIDTokenOptionsAPNSIsSandboxKey : @(isSandboxApp),
|
|
|
- };
|
|
|
+ if (hasFirebaseMessaging) {
|
|
|
+ instanceIDOptions = [self defaultTokenOptions];
|
|
|
}
|
|
|
|
|
|
FIRInstanceID_WEAKIFY(self);
|
|
|
@@ -871,6 +868,11 @@ static FIRInstanceID *gInstanceID;
|
|
|
});
|
|
|
}
|
|
|
|
|
|
+- (BOOL)isDefaultTokenWithAuthorizedEntity:(NSString *)authorizedEntity scope:(NSString *)scope {
|
|
|
+ return [authorizedEntity isEqualToString:self.fcmSenderID] &&
|
|
|
+ [scope isEqualToString:kFIRInstanceIDDefaultTokenScope];
|
|
|
+}
|
|
|
+
|
|
|
#pragma mark - APNS Token
|
|
|
// This should only be triggered from FCM.
|
|
|
- (void)notifyAPNSTokenIsSet:(NSNotification *)notification {
|
|
|
@@ -1117,4 +1119,40 @@ static FIRInstanceID *gInstanceID;
|
|
|
object:nil];
|
|
|
}
|
|
|
|
|
|
+- (void)observeFirebaseMessagingTokenChanges {
|
|
|
+ [[NSNotificationCenter defaultCenter]
|
|
|
+ removeObserver:self
|
|
|
+ name:kFIRInstanceIDMessagingUpdateTokenNotification
|
|
|
+ object:nil];
|
|
|
+ [[NSNotificationCenter defaultCenter]
|
|
|
+ addObserver:self
|
|
|
+ selector:@selector(messagingTokenDidChangeNotificationReceived:)
|
|
|
+ name:kFIRInstanceIDMessagingUpdateTokenNotification
|
|
|
+ object:nil];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)messagingTokenDidChangeNotificationReceived:(NSNotification *)notification {
|
|
|
+ NSString *tokenUpdatedFromMessaging = notification.object;
|
|
|
+ if (!tokenUpdatedFromMessaging || [tokenUpdatedFromMessaging isKindOfClass:[NSString class]]) {
|
|
|
+ self.defaultFCMToken = tokenUpdatedFromMessaging;
|
|
|
+ [self.tokenManager saveDefaultToken:tokenUpdatedFromMessaging
|
|
|
+ withOptions:[self defaultTokenOptions]];
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+- (NSDictionary *)defaultTokenOptions {
|
|
|
+ NSDictionary *tokenOptions = @{};
|
|
|
+ if (self.apnsTokenData) {
|
|
|
+ BOOL isSandboxApp = (self.apnsTokenType == FIRInstanceIDAPNSTokenTypeSandbox);
|
|
|
+ if (self.apnsTokenType == FIRInstanceIDAPNSTokenTypeUnknown) {
|
|
|
+ isSandboxApp = [self isSandboxApp];
|
|
|
+ }
|
|
|
+ tokenOptions = @{
|
|
|
+ kFIRInstanceIDTokenOptionsAPNSKey : self.apnsTokenData,
|
|
|
+ kFIRInstanceIDTokenOptionsAPNSIsSandboxKey : @(isSandboxApp),
|
|
|
+ };
|
|
|
+ }
|
|
|
+ return tokenOptions;
|
|
|
+}
|
|
|
+
|
|
|
@end
|