Skip to content

Fix app launch MCS connection issue #290

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

Merged
merged 2 commits into from
Sep 19, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 23 additions & 3 deletions Firebase/Messaging/FIRMessagingClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#import "FIRMessagingClient.h"

#import "FIRMessagingConnection.h"
#import "FIRMessagingConstants.h"
#import "FIRMessagingDataMessageManager.h"
#import "FIRMessagingDefines.h"
#import "FIRMessagingLogger.h"
Expand Down Expand Up @@ -117,6 +118,11 @@ - (instancetype)initWithDelegate:(id<FIRMessagingClientDelegate>)delegate
_rmq2Manager = rmq2Manager;
_registrar = [[FIRMessagingRegistrar alloc] init];
_connectionTimeoutInterval = kConnectTimeoutInterval;
// Listen for checkin fetch notifications, as
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfinished sentence?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops! Fixed.

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(checkinFetched:)
name:kFIRMessagingCheckinFetchedNotification
object:nil];
}
return self;
}
Expand All @@ -135,6 +141,8 @@ - (void)teardown {

_FIRMessagingDevAssert(self.connection.state == kFIRMessagingConnectionNotConnected, @"Did not disconnect");
[NSObject cancelPreviousPerformRequestsWithTarget:self];

[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)cancelAllRequests {
Expand Down Expand Up @@ -273,12 +281,15 @@ - (void)connect {

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;
}
}
Expand Down Expand Up @@ -316,6 +327,14 @@ - (void)disconnectWithTryToConnectLater:(BOOL)tryToConnectLater {
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

Expand Down Expand Up @@ -465,9 +484,10 @@ - (void)scheduleConnectRetry {
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;
Expand Down
1 change: 1 addition & 0 deletions Firebase/Messaging/FIRMessagingConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ FOUNDATION_EXPORT NSString *const kFIRMessagingRemoteNotificationsProxyEnabledIn
FOUNDATION_EXPORT NSString *const kFIRMessagingApplicationSupportSubDirectory;

// Notifications
FOUNDATION_EXPORT NSString *const kFIRMessagingCheckinFetchedNotification;
FOUNDATION_EXPORT NSString *const kFIRMessagingAPNSTokenNotification;
FOUNDATION_EXPORT NSString *const kFIRMessagingFCMTokenNotification;
FOUNDATION_EXPORT NSString *const kFIRMessagingInstanceIDTokenRefreshNotification __deprecated_msg("Use kFIRMessagingRegistrationTokenRefreshNotification instead");
Expand Down
1 change: 1 addition & 0 deletions Firebase/Messaging/FIRMessagingConstants.m
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
NSString *const kFIRMessagingApplicationSupportSubDirectory = @"Google/FirebaseMessaging";

// Notifications
NSString *const kFIRMessagingCheckinFetchedNotification = @"com.google.gcm.notif-checkin-fetched";
NSString *const kFIRMessagingAPNSTokenNotification = @"com.firebase.iid.notif.apns-token";
NSString *const kFIRMessagingFCMTokenNotification = @"com.firebase.iid.notif.fcm-token";
NSString *const kFIRMessagingInstanceIDTokenRefreshNotification =
Expand Down