Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decode old fcmToken format #12246

Merged
merged 5 commits into from
Jan 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions FirebaseMessaging/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 10.20.0
- [fixed] Fix 10.19.0 regression where the FCM registration token was nil at first app start
after update from 10.19.0 or earlier. (#12245)

# 10.19.0
- [changed] Adopt NSSecureCoding for internal classes. (#12075)

Expand Down
3 changes: 3 additions & 0 deletions FirebaseMessaging/Sources/Token/FIRMessagingTokenInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ NS_ASSUME_NONNULL_BEGIN
/// the cacheTime would be updated.
@property(nonatomic, copy, nullable) NSDate *cacheTime;

/// Indicates the info was stored on the keychain by version 10.18.0 or earlier.
@property(nonatomic, readonly) BOOL needsMigration;

/**
* Initializes a FIRMessagingTokenInfo object with the required parameters. These
* parameters represent all the relevant associated data with a token.
Expand Down
22 changes: 21 additions & 1 deletion FirebaseMessaging/Sources/Token/FIRMessagingTokenInfo.m
paulb777 marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ + (BOOL)supportsSecureCoding {
}

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

id authorizedEntity = [aDecoder decodeObjectForKey:kFIRInstanceIDAuthorizedEntityKey];
Expand Down Expand Up @@ -172,7 +173,25 @@ - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
FIRMessagingAPNSInfo *rawAPNSInfo = [aDecoder decodeObjectOfClasses:classes
forKey:kFIRInstanceIDAPNSInfoKey];
if (rawAPNSInfo && ![rawAPNSInfo isKindOfClass:[FIRMessagingAPNSInfo class]]) {
return nil;
// If the decoder fails to decode a FIRMessagingAPNSInfo, check if this was archived by a
// FirebaseMessaging 10.18.0 or earlier.
// TODO(#12246) This block may be replaced with `rawAPNSInfo = nil` once we're confident all
// users have upgraded to at least 10.19.0. Perhaps, after privacy manifests have been required
// for awhile?
@try {
[NSKeyedUnarchiver setClass:[FIRMessagingAPNSInfo class]
forClassName:@"FIRInstanceIDAPNSInfo"];
paulb777 marked this conversation as resolved.
Show resolved Hide resolved
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
rawAPNSInfo = [NSKeyedUnarchiver unarchiveObjectWithData:(NSData *)rawAPNSInfo];
paulb777 marked this conversation as resolved.
Show resolved Hide resolved
needsMigration = YES;
#pragma clang diagnostic pop
} @catch (NSException *exception) {
FIRMessagingLoggerInfo(kFIRMessagingMessageCodeTokenInfoBadAPNSInfo,
@"Could not parse raw APNS Info while parsing archived token info.");
rawAPNSInfo = nil;
} @finally {
}
}

id cacheTime = [aDecoder decodeObjectForKey:kFIRInstanceIDCacheTimeKey];
Expand All @@ -189,6 +208,7 @@ - (nullable instancetype)initWithCoder:(NSCoder *)aDecoder {
_firebaseAppID = [firebaseAppID copy];
_APNSInfo = [rawAPNSInfo copy];
_cacheTime = cacheTime;
_needsMigration = needsMigration;
}
return self;
}
Expand Down
16 changes: 16 additions & 0 deletions FirebaseMessaging/Sources/Token/FIRMessagingTokenStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,21 @@ - (nullable FIRMessagingTokenInfo *)tokenInfoWithAuthorizedEntity:(NSString *)au
}
// Token infos created from legacy storage don't have appVersion, firebaseAppID, or APNSInfo.
FIRMessagingTokenInfo *tokenInfo = [[self class] tokenInfoFromKeychainItem:item];
if ([tokenInfo needsMigration]) {
[self
saveTokenInfo:tokenInfo
handler:^(NSError *error) {
if (error) {
FIRMessagingLoggerDebug(kFIRMessagingMessageCodeTokenManager001,
@"Failed to migrate token: %@ account: %@ service %@",
tokenInfo, account, service);
} else {
FIRMessagingLoggerDebug(kFIRMessagingMessageCodeTokenManager001,
@"Successful token migration: %@ account: %@ service %@",
tokenInfo, account, service);
}
}];
}
return tokenInfo;
}

Expand Down Expand Up @@ -94,6 +109,7 @@ + (nullable FIRMessagingTokenInfo *)tokenInfoFromKeychainItem:(NSData *)item {
[NSKeyedUnarchiver setClass:[FIRMessagingTokenInfo class]
forClassName:@"FIRInstanceIDTokenInfo"];
tokenInfo = [NSKeyedUnarchiver unarchiveObjectWithData:item];

#pragma clang diagnostic pop

} @catch (NSException *exception) {
Expand Down
Loading