本文介绍了FIRInstanceID.instanceID().token()和Messaging.messaging().fcmToken有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的应用程序中实现Firebase推送通知.在一个教程中,我发现我从 Messaging.messaging().fcmToken 和 SO问题,我找到了这种方法: FIRInstanceID.instanceID().token()

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
}

推荐答案

调用它们中的任何一个都应返回相同的注册令牌.

Calling either of them should return the same registration token.

区别在于 FIRInstanceID 仅具有与注册令牌相关的方法(例如,获取和删除令牌),而 Messaging (又称为 FIRMessaging -命名更改)通常会提供更多方法(例如,订阅主题,发送上游消息).

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

这篇关于FIRInstanceID.instanceID().token()和Messaging.messaging().fcmToken有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:02