问题
I don't understand what's the difference between:
FirebaseAuth.getInstance().currentUser.getIdToken(true).addOnSuccessListener {
Log.d("tag",it.token) // token #1
}
FirebaseInstanceId.getInstance().instanceId.addOnSuccessListener {
Log.d("tag", it.token) //token #2
}
Both of these methods return some token, what is the difference between them?
回答1:
getIdToken(boolean forceRefresh)
Fetches a Firebase Auth ID Token for the user; useful when authenticating against your own backend.
The getIdToken
is in class FirebaseUser
, you use the token to authenticate the user when you are using your own server.
getInstanceId()
returns the id of the app that you downloaded, it is also used in FCM to be able to send notifications to a specific phone that has the app downloaded.
Instance ID is stable except when:
- App deletes Instance ID
- App is restored on a new device
- User uninstalls/reinstall the app
- User clears app data
https://firebase.google.com/docs/reference/android/com/google/firebase/iid/FirebaseInstanceId.html#getInstanceId()
回答2:
Both tokens have very different usages, and very different lifecycles.
Each app installation that uses Firebase Cloud Messaging has its own unique instance ID. So this ID identifies a single app on a since device. If you have multiple apps using FCM on the same phone, each app has its own Instance ID. If you have the same app using FCM on multiple phones, it has a unique Instance ID on each phone. Peter's answer contains a great explanation of when this value may change.
Each time a user signs in to an with Firebase Authentication, they get a ID token. This ID token is valid for an hour and authenticates that the user has signed in to that app. After (slightly less than) an hour, the Firebase SDK refreshes the ID token.
来源:https://stackoverflow.com/questions/54390789/whats-the-difference-between-instanceid-and-getidtoken