diff options
author | Riz <rsattar@gmail.com> | 2017-09-19 15:08:35 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-19 15:08:35 -0700 |
commit | bc99b959e6e593adc3ec324c145c80c8f17934e7 (patch) | |
tree | eef988ab0cbdc7f8f474af0a192444d0735fc5c5 /Firebase/Messaging/FIRMessagingClient.m | |
parent | 637bf879adc17b9609175499ed0b12e51b64ef7b (diff) |
Fix app launch MCS connection issue (#290)
This fixes a bug in the automatic direct channel logic, where a failure to get checkin info from the Instance ID SDK was resulting in a non-retrying failure, causing the automatic connection logic to not retry connecting. A listener is added for a new notification that will be fired from Instance ID. When that notification is fired, `FIRMessagingClient` will re-attempt to connect.
NOTE: The actual fix will not work until the Firebase InstanceID SDK is also updated.
Diffstat (limited to 'Firebase/Messaging/FIRMessagingClient.m')
-rw-r--r-- | Firebase/Messaging/FIRMessagingClient.m | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/Firebase/Messaging/FIRMessagingClient.m b/Firebase/Messaging/FIRMessagingClient.m index c01aecc..13e1775 100644 --- a/Firebase/Messaging/FIRMessagingClient.m +++ b/Firebase/Messaging/FIRMessagingClient.m @@ -17,6 +17,7 @@ #import "FIRMessagingClient.h" #import "FIRMessagingConnection.h" +#import "FIRMessagingConstants.h" #import "FIRMessagingDataMessageManager.h" #import "FIRMessagingDefines.h" #import "FIRMessagingLogger.h" @@ -117,6 +118,12 @@ static NSUInteger FIRMessagingServerPort() { _rmq2Manager = rmq2Manager; _registrar = [[FIRMessagingRegistrar alloc] init]; _connectionTimeoutInterval = kConnectTimeoutInterval; + // Listen for checkin fetch notifications, as connecting to MCS may have failed due to + // missing checkin info (while it was being fetched). + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(checkinFetched:) + name:kFIRMessagingCheckinFetchedNotification + object:nil]; } return self; } @@ -135,6 +142,8 @@ static NSUInteger FIRMessagingServerPort() { _FIRMessagingDevAssert(self.connection.state == kFIRMessagingConnectionNotConnected, @"Did not disconnect"); [NSObject cancelPreviousPerformRequestsWithTarget:self]; + + [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)cancelAllRequests { @@ -273,12 +282,15 @@ static NSUInteger FIRMessagingServerPort() { if (!isRegistrationComplete) { if (![self.registrar tryToLoadValidCheckinInfo]) { + // Checkin info is not available. This may be due to the checkin still being fetched. if (self.connectHandler) { NSError *error = [NSError errorWithFCMErrorCode:kFIRMessagingErrorCodeMissingDeviceID]; self.connectHandler(error); } FIRMessagingLoggerDebug(kFIRMessagingMessageCodeClient009, @"Failed to connect to MCS. No deviceID and secret found."); + // Return for now. If checkin is, in fact, retrieved, the + // |kFIRMessagingCheckinFetchedNotification| will be fired. return; } } @@ -316,6 +328,14 @@ static NSUInteger FIRMessagingServerPort() { self.connectHandler = nil; } +#pragma mark - Checkin Notification +- (void)checkinFetched:(NSNotification *)notification { + // A failed checkin may have been the reason for the connection failure. Attempt a connection + // if the checkin fetched notification is fired. + if (self.stayConnected && !self.isConnected) { + [self connect]; + } +} #pragma mark - Messages @@ -465,9 +485,10 @@ static NSUInteger FIRMessagingServerPort() { FIRMessagingConnectCompletionHandler handler = [self.connectHandler copy]; // disconnect before issuing a callback [self disconnectWithTryToConnectLater:YES]; - NSError *error = [NSError errorWithDomain:@"No internet available, cannot connect to FIRMessaging" - code:kFIRMessagingErrorCodeNetwork - userInfo:nil]; + NSError *error = + [NSError errorWithDomain:@"No internet available, cannot connect to FIRMessaging" + code:kFIRMessagingErrorCodeNetwork + userInfo:nil]; if (handler) { handler(error); self.connectHandler = nil; |