17
17
#import " FIRMessagingClient.h"
18
18
19
19
#import " FIRMessagingConnection.h"
20
+ #import " FIRMessagingConstants.h"
20
21
#import " FIRMessagingDataMessageManager.h"
21
22
#import " FIRMessagingDefines.h"
22
23
#import " FIRMessagingLogger.h"
@@ -117,6 +118,12 @@ - (instancetype)initWithDelegate:(id<FIRMessagingClientDelegate>)delegate
117
118
_rmq2Manager = rmq2Manager;
118
119
_registrar = [[FIRMessagingRegistrar alloc ] init ];
119
120
_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 ];
120
127
}
121
128
return self;
122
129
}
@@ -135,6 +142,8 @@ - (void)teardown {
135
142
136
143
_FIRMessagingDevAssert (self.connection .state == kFIRMessagingConnectionNotConnected , @" Did not disconnect" );
137
144
[NSObject cancelPreviousPerformRequestsWithTarget: self ];
145
+
146
+ [[NSNotificationCenter defaultCenter ] removeObserver: self ];
138
147
}
139
148
140
149
- (void )cancelAllRequests {
@@ -273,12 +282,15 @@ - (void)connect {
273
282
274
283
if (!isRegistrationComplete) {
275
284
if (![self .registrar tryToLoadValidCheckinInfo ]) {
285
+ // Checkin info is not available. This may be due to the checkin still being fetched.
276
286
if (self.connectHandler ) {
277
287
NSError *error = [NSError errorWithFCMErrorCode: kFIRMessagingErrorCodeMissingDeviceID ];
278
288
self.connectHandler (error);
279
289
}
280
290
FIRMessagingLoggerDebug (kFIRMessagingMessageCodeClient009 ,
281
291
@" 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.
282
294
return ;
283
295
}
284
296
}
@@ -316,6 +328,14 @@ - (void)disconnectWithTryToConnectLater:(BOOL)tryToConnectLater {
316
328
self.connectHandler = nil ;
317
329
}
318
330
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
+ }
319
339
320
340
#pragma mark - Messages
321
341
@@ -465,9 +485,10 @@ - (void)scheduleConnectRetry {
465
485
FIRMessagingConnectCompletionHandler handler = [self .connectHandler copy ];
466
486
// disconnect before issuing a callback
467
487
[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 ];
471
492
if (handler) {
472
493
handler (error);
473
494
self.connectHandler = nil ;
0 commit comments