问题
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