Skip to content

Commit 75f5282

Browse files
authored
Decode old fcmToken format (#12246)
1 parent 0fa8498 commit 75f5282

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

FirebaseMessaging/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# 10.20.0
2+
- [fixed] Fix 10.19.0 regression where the FCM registration token was nil at first app start
3+
after update from 10.19.0 or earlier. (#12245)
4+
15
# 10.19.0
26
- [changed] Adopt NSSecureCoding for internal classes. (#12075)
37

FirebaseMessaging/Sources/Token/FIRMessagingTokenInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ NS_ASSUME_NONNULL_BEGIN
5050
/// the cacheTime would be updated.
5151
@property(nonatomic, copy, nullable) NSDate *cacheTime;
5252

53+
/// Indicates the info was stored on the keychain by version 10.18.0 or earlier.
54+
@property(nonatomic, readonly) BOOL needsMigration;
55+
5356
/**
5457
* Initializes a FIRMessagingTokenInfo object with the required parameters. These
5558
* parameters represent all the relevant associated data with a token.

FirebaseMessaging/Sources/Token/FIRMessagingTokenInfo.m

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ + (BOOL)supportsSecureCoding {
140140
}
141141

142142
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
143+
BOOL needsMigration = NO;
143144
// These value cannot be nil
144145

145146
id authorizedEntity = [aDecoder decodeObjectForKey:kFIRInstanceIDAuthorizedEntityKey];
@@ -172,7 +173,25 @@ - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
172173
FIRMessagingAPNSInfo *rawAPNSInfo = [aDecoder decodeObjectOfClasses:classes
173174
forKey:kFIRInstanceIDAPNSInfoKey];
174175
if (rawAPNSInfo && ![rawAPNSInfo isKindOfClass:[FIRMessagingAPNSInfo class]]) {
175-
return nil;
176+
// If the decoder fails to decode a FIRMessagingAPNSInfo, check if this was archived by a
177+
// FirebaseMessaging 10.18.0 or earlier.
178+
// TODO(#12246) This block may be replaced with `rawAPNSInfo = nil` once we're confident all
179+
// users have upgraded to at least 10.19.0. Perhaps, after privacy manifests have been required
180+
// for awhile?
181+
@try {
182+
[NSKeyedUnarchiver setClass:[FIRMessagingAPNSInfo class]
183+
forClassName:@"FIRInstanceIDAPNSInfo"];
184+
#pragma clang diagnostic push
185+
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
186+
rawAPNSInfo = [NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)rawAPNSInfo];
187+
needsMigration = YES;
188+
#pragma clang diagnostic pop
189+
} @catch (NSException *exception) {
190+
FIRMessagingLoggerInfo(kFIRMessagingMessageCodeTokenInfoBadAPNSInfo,
191+
@"Could not parse raw APNS Info while parsing archived token info.");
192+
rawAPNSInfo = nil;
193+
} @finally {
194+
}
176195
}
177196

178197
id cacheTime = [aDecoder decodeObjectForKey:kFIRInstanceIDCacheTimeKey];
@@ -189,6 +208,7 @@ - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
189208
_firebaseAppID = [firebaseAppID copy];
190209
_APNSInfo = [rawAPNSInfo copy];
191210
_cacheTime = cacheTime;
211+
_needsMigration = needsMigration;
192212
}
193213
return self;
194214
}

FirebaseMessaging/Sources/Token/FIRMessagingTokenStore.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,21 @@ - (nullable FIRMessagingTokenInfo *)tokenInfoWithAuthorizedEntity:(NSString *)au
6363
}
6464
// Token infos created from legacy storage don't have appVersion, firebaseAppID, or APNSInfo.
6565
FIRMessagingTokenInfo *tokenInfo = [[self class] tokenInfoFromKeychainItem:item];
66+
if ([tokenInfo needsMigration]) {
67+
[self
68+
saveTokenInfo:tokenInfo
69+
handler:^(NSError *error) {
70+
if (error) {
71+
FIRMessagingLoggerDebug(kFIRMessagingMessageCodeTokenManager001,
72+
@"Failed to migrate token: %@ account: %@ service %@",
73+
tokenInfo, account, service);
74+
} else {
75+
FIRMessagingLoggerDebug(kFIRMessagingMessageCodeTokenManager001,
76+
@"Successful token migration: %@ account: %@ service %@",
77+
tokenInfo, account, service);
78+
}
79+
}];
80+
}
6681
return tokenInfo;
6782
}
6883

@@ -94,6 +109,7 @@ + (nullable FIRMessagingTokenInfo *)tokenInfoFromKeychainItem:(NSData *)item {
94109
[NSKeyedUnarchiver setClass:[FIRMessagingTokenInfo class]
95110
forClassName:@"FIRInstanceIDTokenInfo"];
96111
tokenInfo = [NSKeyedUnarchiver unarchiveObjectWithData:item];
112+
97113
#pragma clang diagnostic pop
98114

99115
} @catch (NSException *exception) {

0 commit comments

Comments
 (0)