aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firebase/Messaging/FIRMessaging.m
diff options
context:
space:
mode:
authorGravatar Riz <rsattar@gmail.com>2017-10-12 16:09:07 -0700
committerGravatar GitHub <noreply@github.com>2017-10-12 16:09:07 -0700
commit2b40693be4c9aa2c94b668fb144c6993f557b8d6 (patch)
treecb1462b6df4132e67a665331def8f4fc9cd78edc /Firebase/Messaging/FIRMessaging.m
parent230c78b5ed7ea80f7ff9aa67c6b12a2794c08718 (diff)
Add simpler delegate method for FCM tokens (#375)
This new delegate method will be called generally once per app start, to always provide a current token. This token may change over time. This simpler method makes integration much simpler, as: * Developers no longer have to check for a current token using the `.fcmToken` property, and also check for token changes using the `-messaging:didRefreshRegistrationToken:` delegate method. * There is a single code path for when a token is available, making operations that depend on a token being available easier to implement. For example, this is the right method to always upload your FCM token to your application server, or to subscribe to topics, etc.
Diffstat (limited to 'Firebase/Messaging/FIRMessaging.m')
-rw-r--r--Firebase/Messaging/FIRMessaging.m76
1 files changed, 62 insertions, 14 deletions
diff --git a/Firebase/Messaging/FIRMessaging.m b/Firebase/Messaging/FIRMessaging.m
index 9d10741..1c31961 100644
--- a/Firebase/Messaging/FIRMessaging.m
+++ b/Firebase/Messaging/FIRMessaging.m
@@ -507,6 +507,42 @@ NSString * const FIRMessagingRegistrationTokenRefreshedNotification =
handler:completion];
}
+#pragma mark - FIRMessagingDelegate helper methods
+- (void)setDelegate:(id<FIRMessagingDelegate>)delegate {
+ _delegate = delegate;
+ [self validateDelegateConformsToTokenAvailabilityMethods];
+}
+
+// Check if the delegate conforms to either |didReceiveRegistrationToken:| or
+// |didRefreshRegistrationToken:|, and display a warning to the developer if not.
+// NOTE: Once |didReceiveRegistrationToken:| can be made a required method, this
+// check can be removed.
+- (void)validateDelegateConformsToTokenAvailabilityMethods {
+ if (self.delegate &&
+ ![self.delegate respondsToSelector:@selector(messaging:didReceiveRegistrationToken:)] &&
+ ![self.delegate respondsToSelector:@selector(messaging:didRefreshRegistrationToken:)]) {
+ FIRMessagingLoggerWarn(kFIRMessagingMessageCodeTokenDelegateMethodsNotImplemented,
+ @"The object %@ does not respond to "
+ @"-messaging:didReceiveRegistrationToken:, nor "
+ @"-messaging:didRefreshRegistrationToken:. Please implement "
+ @"-messaging:didReceiveRegistrationToken: to be provided with an FCM "
+ @"token.", self.delegate.description);
+ }
+}
+
+- (void)notifyDelegateOfFCMTokenAvailability {
+ __weak FIRMessaging *weakSelf = self;
+ if (![NSThread isMainThread]) {
+ dispatch_async(dispatch_get_main_queue(), ^{
+ [weakSelf notifyDelegateOfFCMTokenAvailability];
+ });
+ return;
+ }
+ if ([self.delegate respondsToSelector:@selector(messaging:didReceiveRegistrationToken:)]) {
+ [self.delegate messaging:self didReceiveRegistrationToken:self.defaultFcmToken];
+ }
+}
+
#pragma mark - Application State Changes
- (void)applicationStateChanged {
@@ -719,6 +755,17 @@ NSString * const FIRMessagingRegistrationTokenRefreshedNotification =
#pragma mark - Network
+- (void)onNetworkStatusChanged {
+ if (![self.client isConnected] && [self isNetworkAvailable]) {
+ if (self.client.shouldStayConnected) {
+ FIRMessagingLoggerDebug(kFIRMessagingMessageCodeMessaging014,
+ @"Attempting to establish direct channel.");
+ [self.client retryConnectionImmediately:YES];
+ }
+ [self.pubsub scheduleSync:YES];
+ }
+}
+
- (BOOL)isNetworkAvailable {
FIRReachabilityStatus status = self.reachability.reachabilityStatus;
return (status == kFIRReachabilityViaCellular || status == kFIRReachabilityViaWifi);
@@ -737,19 +784,6 @@ NSString * const FIRMessagingRegistrationTokenRefreshedNotification =
#pragma mark - Notifications
-- (void)onNetworkStatusChanged {
- if (![self.client isConnected] && [self isNetworkAvailable]) {
- if (self.client.shouldStayConnected) {
- FIRMessagingLoggerDebug(kFIRMessagingMessageCodeMessaging014,
- @"Attempting to establish direct channel.");
- [self.client retryConnectionImmediately:YES];
- }
- [self.pubsub scheduleSync:YES];
- }
-}
-
-#pragma mark - Notifications
-
- (void)didReceiveDefaultInstanceIDToken:(NSNotification *)notification {
if (![notification.object isKindOfClass:[NSString class]]) {
FIRMessagingLoggerDebug(kFIRMessagingMessageCodeMessaging015,
@@ -757,7 +791,11 @@ NSString * const FIRMessagingRegistrationTokenRefreshedNotification =
NSStringFromClass([notification.object class]));
return;
}
+ NSString *oldToken = self.defaultFcmToken;
self.defaultFcmToken = [(NSString *)notification.object copy];
+ if (self.defaultFcmToken && ![self.defaultFcmToken isEqualToString:oldToken]) {
+ [self notifyDelegateOfFCMTokenAvailability];
+ }
[self.pubsub scheduleSync:YES];
if (self.shouldEstablishDirectChannel) {
[self updateAutomaticClientConnection];
@@ -771,8 +809,18 @@ NSString * const FIRMessagingRegistrationTokenRefreshedNotification =
// token is fetched, and then notify. This ensures that this token should not
// be nil when the developer accesses it.
if (token != nil) {
+ NSString *oldToken = self.defaultFcmToken;
self.defaultFcmToken = [token copy];
- [self.delegate messaging:self didRefreshRegistrationToken:token];
+ if (self.defaultFcmToken && ![self.defaultFcmToken isEqualToString:oldToken]) {
+ [self notifyDelegateOfFCMTokenAvailability];
+ }
+ // Call deprecated refresh method, because it should still work (until it is removed).
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wdeprecated-declarations"
+ if ([self.delegate respondsToSelector:@selector(messaging:didRefreshRegistrationToken:)]) {
+ [self.delegate messaging:self didRefreshRegistrationToken:token];
+ }
+#pragma clang diagnostic pop
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center postNotificationName:FIRMessagingRegistrationTokenRefreshedNotification object:nil];
}