aboutsummaryrefslogtreecommitdiffhomepage
path: root/Example/Messaging
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 /Example/Messaging
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 'Example/Messaging')
-rw-r--r--Example/Messaging/App/iOS/AppDelegate.swift21
1 files changed, 9 insertions, 12 deletions
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+.