Skip to content

Commit 0582395

Browse files
Should only have only one local cache of token (#7233)
1 parent ded8291 commit 0582395

38 files changed

+205
-1550
lines changed

FirebaseMessaging/Apps/Shared/ContentView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ struct ContentView: View {
6060
}
6161

6262
// MARK: Action buttons
63+
6364
VStack(alignment: .leading) {
6465
Text("getToken")
6566
.fontWeight(.semibold)

FirebaseMessaging/Sources/FIRMessaging.m

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,22 @@
1919
#error FIRMessagingLib should be compiled with ARC.
2020
#endif
2121

22-
#import "GoogleUtilities/AppDelegateSwizzler/Public/GoogleUtilities/GULAppDelegateSwizzler.h"
23-
#import "GoogleUtilities/Environment/Public/GoogleUtilities/GULAppEnvironmentUtil.h"
24-
#import "GoogleUtilities/Reachability/Public/GoogleUtilities/GULReachabilityChecker.h"
25-
#import "GoogleUtilities/UserDefaults/Public/GoogleUtilities/GULUserDefaults.h"
2622
#import "FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessaging.h"
2723
#import "Firebase/InstanceID/Private/FIRInstanceID_Private.h"
28-
#import "FirebaseInstallations/Source/Library/Public/FirebaseInstallations/FirebaseInstallations.h"
2924
#import "FirebaseCore/Sources/Private/FirebaseCoreInternal.h"
25+
#import "FirebaseInstallations/Source/Library/Public/FirebaseInstallations/FirebaseInstallations.h"
3026
#import "FirebaseMessaging/Sources/Interop/FIRMessagingInterop.h"
3127
#import "FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessagingExtensionHelper.h"
28+
#import "GoogleUtilities/AppDelegateSwizzler/Public/GoogleUtilities/GULAppDelegateSwizzler.h"
29+
#import "GoogleUtilities/Environment/Public/GoogleUtilities/GULAppEnvironmentUtil.h"
30+
#import "GoogleUtilities/Reachability/Public/GoogleUtilities/GULReachabilityChecker.h"
31+
#import "GoogleUtilities/UserDefaults/Public/GoogleUtilities/GULUserDefaults.h"
3232
#import "Interop/Analytics/Public/FIRAnalyticsInterop.h"
3333

3434
#import "FirebaseMessaging/Sources/FIRMessagingAnalytics.h"
35+
#import "FirebaseMessaging/Sources/FIRMessagingCode.h"
3536
#import "FirebaseMessaging/Sources/FIRMessagingConstants.h"
3637
#import "FirebaseMessaging/Sources/FIRMessagingContextManagerService.h"
37-
#import "FirebaseMessaging/Sources/FIRMessagingCode.h"
3838
#import "FirebaseMessaging/Sources/FIRMessagingDefines.h"
3939
#import "FirebaseMessaging/Sources/FIRMessagingLogger.h"
4040
#import "FirebaseMessaging/Sources/FIRMessagingPubSub.h"
@@ -106,7 +106,6 @@ @interface FIRMessaging () <GULReachabilityDelegate>
106106

107107
// FIRApp properties
108108
@property(nonatomic, readwrite, strong) NSData *apnsTokenData;
109-
@property(nonatomic, readwrite, strong) NSString *defaultFcmToken;
110109
@property(nonatomic, readwrite, strong) FIRMessagingClient *client;
111110
@property(nonatomic, readwrite, strong) GULReachabilityChecker *reachability;
112111
@property(nonatomic, readwrite, strong) FIRMessagingPubSub *pubsub;
@@ -247,6 +246,11 @@ - (void)didCompleteConfigure {
247246
completion:^(NSString *_Nullable FCMToken, NSError *_Nullable error){
248247
}];
249248
}
249+
// Set the default FCM token, there's an issue that FIRApp configure
250+
// happens before developers able to set the delegate
251+
// Hence first token set must be happen here after listener is set
252+
// TODO(chliangGoogle) Need to investigate better solution.
253+
[self updateDefaultFCMToken:self.FCMToken];
250254
}];
251255
} else if (self.isAutoInitEnabled) {
252256
// When there is no cached token, must check auto init is enabled.
@@ -315,7 +319,7 @@ - (void)setupNotificationListeners {
315319
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
316320
[center removeObserver:self];
317321
[center addObserver:self
318-
selector:@selector(defaultInstanceIDTokenWasRefreshed:)
322+
selector:@selector(defaultFCMTokenWasRefreshed:)
319323
name:kFIRMessagingRegistrationTokenRefreshNotification
320324
object:nil];
321325
}
@@ -326,7 +330,7 @@ - (void)setupRmqManager {
326330
}
327331

328332
- (void)setupTopics {
329-
self.pubsub = [[FIRMessagingPubSub alloc] initWithTokenManager:_tokenManager];
333+
self.pubsub = [[FIRMessagingPubSub alloc] initWithTokenManager:self.tokenManager];
330334
}
331335

332336
- (void)setupSyncMessageManager {
@@ -540,7 +544,8 @@ - (void)setAutoInitEnabled:(BOOL)autoInitEnabled {
540544

541545
- (NSString *)FCMToken {
542546
// Gets the current default token, and requets a new one if it doesn't exist.
543-
return [[FIRMessaging messaging].tokenManager tokenAndRequestIfNotExist];
547+
NSString *token = [self.tokenManager tokenAndRequestIfNotExist];
548+
return token;
544549
}
545550

546551
- (void)tokenWithCompletion:(FIRMessagingFCMTokenFetchCompletion)completion {
@@ -625,7 +630,7 @@ - (void)deleteFCMTokenForSenderID:(nonnull NSString *)senderID
625630

626631
- (void)deleteDataWithCompletion:(void (^)(NSError *_Nullable))completion {
627632
FIRMessaging_WEAKIFY(self);
628-
[self.tokenManager deleteWithHandler:^(NSError * error) {
633+
[self.tokenManager deleteWithHandler:^(NSError *error) {
629634
FIRMessaging_STRONGIFY(self);
630635
if (error) {
631636
if (completion) {
@@ -637,8 +642,7 @@ - (void)deleteDataWithCompletion:(void (^)(NSError *_Nullable))completion {
637642
// enabled.
638643
if ([self isAutoInitEnabled]) {
639644
// Deletion succeeds! Requesting new checkin, IID and token.
640-
[self tokenWithCompletion:^(NSString *_Nullable token,
641-
NSError *_Nullable error) {
645+
[self tokenWithCompletion:^(NSString *_Nullable token, NSError *_Nullable error) {
642646
if (completion) {
643647
completion(error);
644648
}
@@ -829,20 +833,23 @@ - (FIRMessagingNetworkStatus)networkType {
829833

830834
#pragma mark - Notifications
831835

832-
- (void)defaultInstanceIDTokenWasRefreshed:(NSNotification *)notification {
836+
- (void)defaultFCMTokenWasRefreshed:(NSNotification *)notification {
833837
if (notification.object && ![notification.object isKindOfClass:[NSString class]]) {
834838
FIRMessagingLoggerDebug(kFIRMessagingMessageCodeMessaging015,
835839
@"Invalid default FCM token type %@",
836840
NSStringFromClass([notification.object class]));
837841
return;
838842
}
839-
// Retrieve the Instance ID default token, and should notify delegate and
840-
// trigger notification as long as the token is different from previous state.
841-
NSString *oldToken = self.defaultFcmToken;
842-
self.defaultFcmToken = [(NSString *)notification.object copy];
843-
if ((self.defaultFcmToken.length && oldToken.length &&
844-
![self.defaultFcmToken isEqualToString:oldToken]) ||
845-
self.defaultFcmToken.length != oldToken.length) {
843+
NSString *newToken = [(NSString *)notification.object copy];
844+
[self updateDefaultFCMToken:newToken];
845+
}
846+
847+
- (void)updateDefaultFCMToken:(NSString *)defaultFCMToken {
848+
NSString *oldToken = self.tokenManager.defaultFCMToken;
849+
NSString *newToken = defaultFCMToken;
850+
if ([self.tokenManager hasTokenChangedFromOldToken:oldToken toNewToken:newToken]) {
851+
// Make sure to set default token first before notifying others.
852+
[self.tokenManager saveDefaultTokenInfoInKeychain:newToken];
846853
[self notifyDelegateOfFCMTokenAvailability];
847854
[self.pubsub scheduleSync:YES];
848855
}
@@ -857,12 +864,12 @@ - (void)notifyDelegateOfFCMTokenAvailability {
857864
return;
858865
}
859866
if ([self.delegate respondsToSelector:@selector(messaging:didReceiveRegistrationToken:)]) {
860-
[self.delegate messaging:self didReceiveRegistrationToken:self.defaultFcmToken];
867+
[self.delegate messaging:self didReceiveRegistrationToken:self.tokenManager.defaultFCMToken];
861868
}
862869
// Should always trigger the token refresh notification when the delegate method is called
863870
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
864871
[center postNotificationName:FIRMessagingRegistrationTokenRefreshedNotification
865-
object:self.defaultFcmToken];
872+
object:self.tokenManager.defaultFCMToken];
866873
}
867874

868875
#pragma mark - Application Support Directory

FirebaseMessaging/Sources/FIRMessagingClient.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
#import <FirebaseMessaging/FIRMessaging.h>
17+
#import "FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessaging.h"
1818

1919
@class GULReachabilityChecker;
2020
@class GPBMessage;

FirebaseMessaging/Sources/FIRMessagingPubSub.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616

1717
#import "FirebaseMessaging/Sources/FIRMessagingPubSub.h"
1818

19-
#import "GoogleUtilities/Environment/Public/GoogleUtilities/GULSecureCoding.h"
20-
#import "GoogleUtilities/UserDefaults/Public/GoogleUtilities/GULUserDefaults.h"
2119
#import "Firebase/InstanceID/Private/FIRInstanceID_Private.h"
2220
#import "FirebaseMessaging/Sources/Public/FirebaseMessaging/FIRMessaging.h"
21+
#import "GoogleUtilities/Environment/Public/GoogleUtilities/GULSecureCoding.h"
22+
#import "GoogleUtilities/UserDefaults/Public/GoogleUtilities/GULUserDefaults.h"
2323

2424
#import "FirebaseMessaging/Sources/FIRMessagingDefines.h"
2525
#import "FirebaseMessaging/Sources/FIRMessagingLogger.h"

FirebaseMessaging/Sources/FIRMessagingUtilities.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616

1717
#import <Foundation/Foundation.h>
1818

19-
20-
2119
#pragma mark - URL Helpers
2220

2321
FOUNDATION_EXPORT NSString *FIRMessagingTokenRegisterServer(void);

FirebaseMessaging/Sources/FIRMessagingUtilities.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
#import "FirebaseMessaging/Sources/FIRMessagingUtilities.h"
1818

1919
#import "FirebaseCore/Sources/Public/FirebaseCore/FIROptions.h"
20-
#import "GoogleUtilities/UserDefaults/Public/GoogleUtilities/GULUserDefaults.h"
2120
#import "FirebaseMessaging/Sources/FIRMessagingLogger.h"
2221
#import "GoogleUtilities/Environment/Public/GoogleUtilities/GULAppEnvironmentUtil.h"
23-
22+
#import "GoogleUtilities/UserDefaults/Public/GoogleUtilities/GULUserDefaults.h"
2423

2524
static const uint64_t kBytesToMegabytesDivisor = 1024 * 1024LL;
2625
NSString *const kFIRMessagingInstanceIDUserDefaultsKeyLocale =

FirebaseMessaging/Sources/FIRMessaging_Private.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ FOUNDATION_EXPORT NSString *const kFIRMessagingPlistUseMessagingDelegate;
3434

3535
#pragma mark - Private API
3636

37-
- (NSString *)defaultFcmToken;
3837
- (FIRMessagingPubSub *)pubsub;
3938

4039
- (BOOL)isNetworkAvailable;

FirebaseMessaging/Sources/Token/FIRMessagingAPNSInfo.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
#import "FIRMessagingAPNSInfo.h"
17+
#import "FirebaseMessaging/Sources/Token/FIRMessagingAPNSInfo.h"
1818

19-
#import "FIRMessagingConstants.h"
19+
#import "FirebaseMessaging/Sources/FIRMessagingConstants.h"
2020

2121
/// The key used to find the APNs device token in an archive.
2222
static NSString *const kFIRInstanceIDAPNSInfoTokenKey = @"device_token";

FirebaseMessaging/Sources/Token/FIRMessagingAuthKeyChain.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
#import "FIRMessagingAuthKeyChain.h"
18-
#import "FIRMessagingKeychain.h"
19-
#import "FIRMessagingLogger.h"
17+
#import "FirebaseMessaging/Sources/Token/FIRMessagingAuthKeyChain.h"
18+
#import "FirebaseMessaging/Sources/FIRMessagingLogger.h"
19+
#import "FirebaseMessaging/Sources/Token/FIRMessagingKeychain.h"
2020

2121
/**
2222
* The error type representing why we couldn't read data from the keychain.

FirebaseMessaging/Sources/Token/FIRMessagingAuthService.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

1717
#import <Foundation/Foundation.h>
18-
#import "FIRMessagingCheckinService.h"
18+
#import "FirebaseMessaging/Sources/Token/FIRMessagingCheckinService.h"
1919

2020
NS_ASSUME_NONNULL_BEGIN
2121

0 commit comments

Comments
 (0)