Skip to content

Commit 6ffc845

Browse files
Check token is different from cache and storage before updating it (#7223)
1 parent 68df11c commit 6ffc845

File tree

5 files changed

+129
-9
lines changed

5 files changed

+129
-9
lines changed

Example/InstanceID/Tests/FIRInstanceIDTest.m

Lines changed: 105 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ @interface FIRInstanceID (ExposedForTest)
5151
@property(nonatomic, readwrite, strong) FIRInstallations *installations;
5252
@property(nonatomic, readwrite, copy) NSString *fcmSenderID;
5353
@property(nonatomic, readwrite, copy) NSString *firebaseAppID;
54+
@property(nonatomic, readwrite, copy) NSString *defaultFCMToken;
5455

5556
- (NSInteger)retryIntervalToFetchDefaultToken;
5657
- (BOOL)isFCMAutoInitEnabled;
@@ -651,7 +652,6 @@ - (void)testDefaultToken_callbackInvokedForUnchangedToken {
651652
return obj != nil;
652653
}]];
653654

654-
__block NSInteger notificationPostCount = 0;
655655
__block NSString *notificationToken = nil;
656656

657657
// Fetch token once to store token state
@@ -663,8 +663,6 @@ - (void)testDefaultToken_callbackInvokedForUnchangedToken {
663663
usingBlock:^(NSNotification *_Nonnull note) {
664664
// Should have saved token to cache
665665
cachedTokenInfo = sTokenInfo;
666-
667-
notificationPostCount++;
668666
notificationToken = [[self.instanceID token] copy];
669667
[defaultTokenExpectation fulfill];
670668
}];
@@ -721,7 +719,6 @@ - (void)testDefaultTokenFetch_returnValidToken {
721719
return obj != nil;
722720
}]];
723721

724-
__block int notificationPostCount = 0;
725722
__block NSString *notificationToken = nil;
726723

727724
NSString *notificationName = kFIRInstanceIDTokenRefreshNotification;
@@ -733,7 +730,6 @@ - (void)testDefaultTokenFetch_returnValidToken {
733730
// Should have saved token to cache
734731
cachedTokenInfo = sTokenInfo;
735732

736-
notificationPostCount++;
737733
notificationToken = [[self.instanceID token] copy];
738734
[defaultTokenExpectation fulfill];
739735
}];
@@ -800,6 +796,7 @@ - (void)testDefaultTokenFetch_retryFetchToken {
800796
__block NSString *notificationToken = nil;
801797

802798
NSString *notificationName = kFIRInstanceIDTokenRefreshNotification;
799+
803800
self.tokenRefreshNotificationObserver = [[NSNotificationCenter defaultCenter]
804801
addObserverForName:notificationName
805802
object:nil
@@ -1328,6 +1325,109 @@ - (void)testInstanceIDDelete_keyChainError {
13281325
OCMVerifyAll(self.mockTokenManager);
13291326
}
13301327

1328+
- (void)testRefreshDifferentTokenFromMessaging {
1329+
_instanceID.defaultFCMToken = kToken;
1330+
XCTAssertEqualObjects(_instanceID.defaultFCMToken, kToken);
1331+
NSString *newTokenFromMessaging = @"a_new_token_from_messaging";
1332+
FIRInstanceIDTokenInfo *cachedTokenInfo =
1333+
[[FIRInstanceIDTokenInfo alloc] initWithAuthorizedEntity:kAuthorizedEntity
1334+
scope:kFIRInstanceIDDefaultTokenScope
1335+
token:kToken
1336+
appVersion:@""
1337+
firebaseAppID:kGoogleAppID];
1338+
OCMStub([self.mockTokenManager
1339+
cachedTokenInfoWithAuthorizedEntity:kAuthorizedEntity
1340+
scope:kFIRInstanceIDDefaultTokenScope])
1341+
.andReturn(cachedTokenInfo);
1342+
1343+
OCMExpect([self.mockTokenManager saveDefaultToken:newTokenFromMessaging
1344+
withOptions:[OCMArg any]]);
1345+
[[NSNotificationCenter defaultCenter]
1346+
postNotificationName:kFIRInstanceIDMessagingUpdateTokenNotification
1347+
object:newTokenFromMessaging];
1348+
OCMVerifyAll(self.mockTokenManager);
1349+
XCTAssertEqualObjects(_instanceID.defaultFCMToken, newTokenFromMessaging);
1350+
}
1351+
1352+
- (void)testRefreshTheSameTokenFromMessaging {
1353+
_instanceID.defaultFCMToken = kToken;
1354+
XCTAssertEqualObjects(_instanceID.defaultFCMToken, kToken);
1355+
1356+
NSString *newTokenFromMessaging = kToken;
1357+
FIRInstanceIDTokenInfo *cachedTokenInfo =
1358+
[[FIRInstanceIDTokenInfo alloc] initWithAuthorizedEntity:kAuthorizedEntity
1359+
scope:kFIRInstanceIDDefaultTokenScope
1360+
token:kToken
1361+
appVersion:@""
1362+
firebaseAppID:kGoogleAppID];
1363+
OCMStub([self.mockTokenManager
1364+
cachedTokenInfoWithAuthorizedEntity:kAuthorizedEntity
1365+
scope:kFIRInstanceIDDefaultTokenScope])
1366+
.andReturn(cachedTokenInfo);
1367+
1368+
OCMReject([self.mockTokenManager saveDefaultToken:newTokenFromMessaging
1369+
withOptions:[OCMArg any]]);
1370+
[[NSNotificationCenter defaultCenter]
1371+
postNotificationName:kFIRInstanceIDMessagingUpdateTokenNotification
1372+
object:newTokenFromMessaging];
1373+
OCMVerifyAll(self.mockTokenManager);
1374+
XCTAssertEqualObjects(_instanceID.defaultFCMToken, newTokenFromMessaging);
1375+
}
1376+
1377+
- (void)testRefreshDifferentTokenInInstanceIDStorage {
1378+
_instanceID.defaultFCMToken = kToken;
1379+
XCTAssertEqualObjects(_instanceID.defaultFCMToken, kToken);
1380+
// New token from messaging is the same as local cache in InstanceID
1381+
// But the token in InstanceID storage is different
1382+
NSString *newTokenFromMessaging = kToken;
1383+
1384+
FIRInstanceIDTokenInfo *cachedTokenInfo =
1385+
[[FIRInstanceIDTokenInfo alloc] initWithAuthorizedEntity:kAuthorizedEntity
1386+
scope:kFIRInstanceIDDefaultTokenScope
1387+
token:@"a_outdated_token_in_storage"
1388+
appVersion:@""
1389+
firebaseAppID:kGoogleAppID];
1390+
OCMStub([self.mockTokenManager
1391+
cachedTokenInfoWithAuthorizedEntity:kAuthorizedEntity
1392+
scope:kFIRInstanceIDDefaultTokenScope])
1393+
.andReturn(cachedTokenInfo);
1394+
1395+
OCMExpect([self.mockTokenManager saveDefaultToken:newTokenFromMessaging
1396+
withOptions:[OCMArg any]]);
1397+
[[NSNotificationCenter defaultCenter]
1398+
postNotificationName:kFIRInstanceIDMessagingUpdateTokenNotification
1399+
object:newTokenFromMessaging];
1400+
OCMVerifyAll(self.mockTokenManager);
1401+
XCTAssertEqualObjects(_instanceID.defaultFCMToken, newTokenFromMessaging);
1402+
}
1403+
1404+
- (void)testRefreshNullTokenFromMessaging {
1405+
_instanceID.defaultFCMToken = kToken;
1406+
XCTAssertEqualObjects(_instanceID.defaultFCMToken, kToken);
1407+
// New token from messaging is the same as local cache in InstanceID
1408+
// But the token in InstanceID storage is different
1409+
NSString *newTokenFromMessaging = nil;
1410+
1411+
FIRInstanceIDTokenInfo *cachedTokenInfo =
1412+
[[FIRInstanceIDTokenInfo alloc] initWithAuthorizedEntity:kAuthorizedEntity
1413+
scope:kFIRInstanceIDDefaultTokenScope
1414+
token:kToken
1415+
appVersion:@""
1416+
firebaseAppID:kGoogleAppID];
1417+
OCMStub([self.mockTokenManager
1418+
cachedTokenInfoWithAuthorizedEntity:kAuthorizedEntity
1419+
scope:kFIRInstanceIDDefaultTokenScope])
1420+
.andReturn(cachedTokenInfo);
1421+
1422+
OCMExpect([self.mockTokenManager saveDefaultToken:newTokenFromMessaging
1423+
withOptions:[OCMArg any]]);
1424+
[[NSNotificationCenter defaultCenter]
1425+
postNotificationName:kFIRInstanceIDMessagingUpdateTokenNotification
1426+
object:newTokenFromMessaging];
1427+
OCMVerifyAll(self.mockTokenManager);
1428+
XCTAssertEqualObjects(_instanceID.defaultFCMToken, newTokenFromMessaging);
1429+
}
1430+
13311431
#pragma mark - Private Helpers
13321432

13331433
- (void)stubInstallationsToReturnValidID {

Firebase/InstanceID/CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
# Unreleased -- 7.0.0
1+
# Unreleased
2+
- [changed] Added a check on whether the default token has been changed from Messaging before writing to storage. (#7223)
3+
4+
# 2020-10 -- 7.0.0
25
- [changed] Deprecated private `-[FIRInstanceID appInstanceID:]` method was removed. (#4486)
36
- [fixed] Fixed an issue that APNS token is not sent in token request when there's a delay of getting the APNS token from Apple. (#6553)
47

Firebase/InstanceID/FIRInstanceID.m

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,9 +1127,22 @@ - (void)observeFirebaseMessagingTokenChanges {
11271127
- (void)messagingTokenDidChangeNotificationReceived:(NSNotification *)notification {
11281128
NSString *tokenUpdatedFromMessaging = notification.object;
11291129
if (!tokenUpdatedFromMessaging || [tokenUpdatedFromMessaging isKindOfClass:[NSString class]]) {
1130-
self.defaultFCMToken = tokenUpdatedFromMessaging;
1131-
[self.tokenManager saveDefaultToken:tokenUpdatedFromMessaging
1132-
withOptions:[self defaultTokenOptions]];
1130+
// Check the token from storage along with local value.
1131+
FIRInstanceIDTokenInfo *cachedTokenInfo =
1132+
[self.tokenManager cachedTokenInfoWithAuthorizedEntity:self.fcmSenderID
1133+
scope:kFIRInstanceIDDefaultTokenScope];
1134+
NSString *cachedToken = cachedTokenInfo.token;
1135+
1136+
if (self.defaultFCMToken.length != tokenUpdatedFromMessaging.length ||
1137+
cachedToken.length != tokenUpdatedFromMessaging.length ||
1138+
(self.defaultFCMToken.length && tokenUpdatedFromMessaging.length &&
1139+
![self.defaultFCMToken isEqualToString:tokenUpdatedFromMessaging]) ||
1140+
(cachedToken.length && tokenUpdatedFromMessaging.length &&
1141+
![cachedToken isEqualToString:tokenUpdatedFromMessaging])) {
1142+
self.defaultFCMToken = tokenUpdatedFromMessaging;
1143+
[self.tokenManager saveDefaultToken:tokenUpdatedFromMessaging
1144+
withOptions:[self defaultTokenOptions]];
1145+
}
11331146
}
11341147
}
11351148

FirebaseMessaging/Apps/Shared/ContentView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ struct ContentView: View {
4848
// simulator renders a single truncated line even though the Preview renders it
4949
// appropriately. Potentially a bug in the simulator?
5050
.layoutPriority(1)
51+
.lineLimit(7)
5152
}
5253
NavigationLink(destination: SettingsView()) {
5354
Text("Settings")

FirebaseMessaging/Apps/Shared/SceneDelegate.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,8 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate, MessagingDelegate {
7373

7474
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
7575
identity.token = fcmToken
76+
print("=============================\n")
77+
print("Did refresh token:\n", identity.token ?? "")
78+
print("\n=============================\n")
7679
}
7780
}

0 commit comments

Comments
 (0)