Im creating app with Facebook sign in option. I did everything so far, but having problem with the result after from the Facebook App. I followed Google's documentation, but that one is not updated with the newly deprecated onActivityResult function. Now, I have already used the new way of getting result from another activity from this. However, this is not applicable on the Facebook sign in implementation due to not using the the registerForActivityResult launcher, but it uses its own callback manager.
Has anyone come up with a solution to this problem? Maybe I'm missing something out.
callbackManager = CallbackManager.Factory.create()
signInWithFacebook.setReadPermissions("email", "public_profile")
signInWithFacebook.registerCallback(
callbackManager,
object : FacebookCallback<LoginResult> {
override fun onSuccess(result: LoginResult) {
handleFacebookAccessToken(result.accessToken)
}
override fun onCancel() {
}
override fun onError(error: FacebookException?) {
}
})
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
callbackManager.onActivityResult(requestCode, resultCode, data)
}
private fun handleFacebookAccessToken(token: AccessToken) {
val credential = FacebookAuthProvider.getCredential(token.token)
auth.signInWithCredential(credential)
.addOnCompleteListener(OnCompleteListener {
if (it.isSuccessful) {
} else {
}
})
}
Using this(https://github.com/facebook/facebook-android-sdk/issues/875) Github issue I found your answer and here is summarize
First of all, initialize callback manager
val callbackManager: CallbackManager = CallbackManager.Factory.create()
then register callback using a login manager
LoginManager.getInstance().registerCallback(callbackManager,
object : FacebookCallback<LoginResult?> {
override fun onSuccess(result: LoginResult?) {
if (result?.accessToken != null) {
val accessToken = result.accessToken.token
}
}
override fun onCancel() {
}
override fun onError(error: FacebookException) {
}
})
on your custom Facebook button, you have to setOnClickListner
binding.btnFacebook.setOnClickListener {
LoginManager.getInstance().logInWithReadPermissions(
this,
loginViewModel.callbackManager,
listOf("public_profile", "email")
)
}
what we have to change for onActivityResult Deprecated
Previously
binding.btnFacebook.setOnClickListener {
LoginManager.getInstance().logInWithReadPermissions(
this,
listOf("public_profile", "email")
)
}
Now
binding.btnFacebook.setOnClickListener {
LoginManager.getInstance().logInWithReadPermissions(
this,
loginViewModel.callbackManager, //we added callback here according to new sdk 12.0.0 version of facebook
listOf("public_profile", "email")
)
}
Now I am using Facebook SDK version
//facebook login
implementation 'com.facebook.android:facebook-login:12.0.0'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With