Skip to content

Commit 4799980

Browse files
authored
Fix app launch MCS connection issue (firebase#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.
1 parent bb6af00 commit 4799980

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

Firebase/Messaging/FIRMessagingClient.m

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#import "FIRMessagingClient.h"
1818

1919
#import "FIRMessagingConnection.h"
20+
#import "FIRMessagingConstants.h"
2021
#import "FIRMessagingDataMessageManager.h"
2122
#import "FIRMessagingDefines.h"
2223
#import "FIRMessagingLogger.h"
@@ -117,6 +118,12 @@ - (instancetype)initWithDelegate:(id<FIRMessagingClientDelegate>)delegate
117118
_rmq2Manager = rmq2Manager;
118119
_registrar = [[FIRMessagingRegistrar alloc] init];
119120
_connectionTimeoutInterval = kConnectTimeoutInterval;
121+
// Listen for checkin fetch notifications, as connecting to MCS may have failed due to
122+
// missing checkin info (while it was being fetched).
123+
[[NSNotificationCenter defaultCenter] addObserver:self
124+
selector:@selector(checkinFetched:)
125+
name:kFIRMessagingCheckinFetchedNotification
126+
object:nil];
120127
}
121128
return self;
122129
}
@@ -135,6 +142,8 @@ - (void)teardown {
135142

136143
_FIRMessagingDevAssert(self.connection.state == kFIRMessagingConnectionNotConnected, @"Did not disconnect");
137144
[NSObject cancelPreviousPerformRequestsWithTarget:self];
145+
146+
[[NSNotificationCenter defaultCenter] removeObserver:self];
138147
}
139148

140149
- (void)cancelAllRequests {
@@ -273,12 +282,15 @@ - (void)connect {
273282

274283
if (!isRegistrationComplete) {
275284
if (![self.registrar tryToLoadValidCheckinInfo]) {
285+
// Checkin info is not available. This may be due to the checkin still being fetched.
276286
if (self.connectHandler) {
277287
NSError *error = [NSError errorWithFCMErrorCode:kFIRMessagingErrorCodeMissingDeviceID];
278288
self.connectHandler(error);
279289
}
280290
FIRMessagingLoggerDebug(kFIRMessagingMessageCodeClient009,
281291
@"Failed to connect to MCS. No deviceID and secret found.");
292+
// Return for now. If checkin is, in fact, retrieved, the
293+
// |kFIRMessagingCheckinFetchedNotification| will be fired.
282294
return;
283295
}
284296
}
@@ -316,6 +328,14 @@ - (void)disconnectWithTryToConnectLater:(BOOL)tryToConnectLater {
316328
self.connectHandler = nil;
317329
}
318330

331+
#pragma mark - Checkin Notification
332+
- (void)checkinFetched:(NSNotification *)notification {
333+
// A failed checkin may have been the reason for the connection failure. Attempt a connection
334+
// if the checkin fetched notification is fired.
335+
if (self.stayConnected && !self.isConnected) {
336+
[self connect];
337+
}
338+
}
319339

320340
#pragma mark - Messages
321341

@@ -465,9 +485,10 @@ - (void)scheduleConnectRetry {
465485
FIRMessagingConnectCompletionHandler handler = [self.connectHandler copy];
466486
// disconnect before issuing a callback
467487
[self disconnectWithTryToConnectLater:YES];
468-
NSError *error = [NSError errorWithDomain:@"No internet available, cannot connect to FIRMessaging"
469-
code:kFIRMessagingErrorCodeNetwork
470-
userInfo:nil];
488+
NSError *error =
489+
[NSError errorWithDomain:@"No internet available, cannot connect to FIRMessaging"
490+
code:kFIRMessagingErrorCodeNetwork
491+
userInfo:nil];
471492
if (handler) {
472493
handler(error);
473494
self.connectHandler = nil;

Firebase/Messaging/FIRMessagingConstants.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ FOUNDATION_EXPORT NSString *const kFIRMessagingRemoteNotificationsProxyEnabledIn
4747
FOUNDATION_EXPORT NSString *const kFIRMessagingApplicationSupportSubDirectory;
4848

4949
// Notifications
50+
FOUNDATION_EXPORT NSString *const kFIRMessagingCheckinFetchedNotification;
5051
FOUNDATION_EXPORT NSString *const kFIRMessagingAPNSTokenNotification;
5152
FOUNDATION_EXPORT NSString *const kFIRMessagingFCMTokenNotification;
5253
FOUNDATION_EXPORT NSString *const kFIRMessagingInstanceIDTokenRefreshNotification __deprecated_msg("Use kFIRMessagingRegistrationTokenRefreshNotification instead");

Firebase/Messaging/FIRMessagingConstants.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
NSString *const kFIRMessagingApplicationSupportSubDirectory = @"Google/FirebaseMessaging";
4242

4343
// Notifications
44+
NSString *const kFIRMessagingCheckinFetchedNotification = @"com.google.gcm.notif-checkin-fetched";
4445
NSString *const kFIRMessagingAPNSTokenNotification = @"com.firebase.iid.notif.apns-token";
4546
NSString *const kFIRMessagingFCMTokenNotification = @"com.firebase.iid.notif.fcm-token";
4647
NSString *const kFIRMessagingInstanceIDTokenRefreshNotification =

0 commit comments

Comments
 (0)