What is the difference between FIRInstanceID.instanceID().token() and Messaging.messaging().fcmToken?

不羁岁月 提交于 2021-02-08 14:13:48

问题


I am implementing Firebase push notifications in my app. In one tutorial I find that I get the token from the Messaging.messaging().fcmToken and in this SO question I find this approach: FIRInstanceID.instanceID().token()

What is the difference between them? My only goal is to be able to send my backend guys the token so they can recognize me in the DB for push notifications. Currently my code that generates the token is this:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {

    if #available(iOS 10.0, *) {
        // For iOS 10 display notification (sent via APNS)
        UNUserNotificationCenter.current().delegate = self
        let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
        UNUserNotificationCenter.current().requestAuthorization(
            options: authOptions,
            completionHandler: {_, _ in })
        // For iOS 10 data message (sent via FCM
        Messaging.messaging().delegate = self
    } else {
        let settings: UIUserNotificationSettings =
            UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
        application.registerUserNotificationSettings(settings)
    }

    application.registerForRemoteNotifications()

    FirebaseApp.configure()
    let token = Messaging.messaging().fcmToken
    setFCM(token: token ?? "UNDEFINED") //a function that saves it in user defaults.
    print("FCM token: \(token ?? "")")
    return true
}

回答1:


Calling either of them should return the same registration token.

The difference is that FIRInstanceID only has methods related to the registration token (e.g. getting and deleting the token), while Messaging (aka FIRMessaging -- naming changes) in general provides more methods (e.g. subscribing to topics, sending upstream messages).



来源:https://stackoverflow.com/questions/44615817/what-is-the-difference-between-firinstanceid-instanceid-token-and-messaging

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!