Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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
}
like image 959
Eyzuky Avatar asked Jun 18 '17 14:06

Eyzuky


1 Answers

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).

like image 68
AL. Avatar answered Nov 08 '22 23:11

AL.