From 2b40693be4c9aa2c94b668fb144c6993f557b8d6 Mon Sep 17 00:00:00 2001 From: Riz Date: Thu, 12 Oct 2017 16:09:07 -0700 Subject: 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. --- Example/Messaging/App/iOS/AppDelegate.swift | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) (limited to 'Example/Messaging') diff --git a/Example/Messaging/App/iOS/AppDelegate.swift b/Example/Messaging/App/iOS/AppDelegate.swift index aafd844..32b55ed 100644 --- a/Example/Messaging/App/iOS/AppDelegate.swift +++ b/Example/Messaging/App/iOS/AppDelegate.swift @@ -66,19 +66,9 @@ class AppDelegate: UIResponder, UIApplicationDelegate { // register for remote notifications NotificationsController.shared.registerForUserFacingNotificationsFor(application) } - - printFCMToken() return true } - func printFCMToken() { - if let token = Messaging.messaging().fcmToken { - print("FCM Token: \(token)") - } else { - print("FCM Token: nil") - } - } - func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { print("APNS Token: \(deviceToken.hexByteString)") @@ -116,8 +106,15 @@ class AppDelegate: UIResponder, UIApplicationDelegate { } extension AppDelegate: MessagingDelegate { - func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) { - printFCMToken() + // FCM tokens are always provided here. It is called generally during app start, but may be called + // more than once, if the token is invalidated or updated. This is the right spot to upload this + // token to your application server, or to subscribe to any topics. + func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) { + if let token = Messaging.messaging().fcmToken { + print("FCM Token: \(token)") + } else { + print("FCM Token: nil") + } } // Direct channel data messages are delivered here, on iOS 10.0+. -- cgit v1.2.3