Facebook 使用 Firebase 登录后,如何显示带有 displayName 的吐司消息?
How to display a toast message with the displayName after Facebook login with Firebase?
在我的应用程序中,可以使用 Facebook 登录,在成功登录后,我想显示一条 toast 消息,上面写着 "Welcome back, username (which is the displayName)"。我设法显示了一条消息,但没有用户名,因为我不知道如何从 Firebase 获取它并在登录后显示它。
这是处理 Facebook 登录的代码:
private void handleFacebookAccessToken(AccessToken token) {
Log.d(TAG, "handleFacebookAccessToken:" + token);
progressBar.setVisibility(View.VISIBLE);
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
// Todo make a toast with the username
Toast.makeText(SignInActivity.this, "Welcome back", Toast.LENGTH_SHORT).show();
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
Toast.makeText(SignInActivity.this, "Error.",
Toast.LENGTH_LONG).show();
updateUI(null);
}
progressBar.setVisibility(View.INVISIBLE);
}
});
}
获得 Facebook 访问令牌后,您可以使用 GraphApi 获取用户的附加信息:
val request = GraphRequest.newMeRequest(
accessToken
) { user, _ ->
try {
val name = user.getString("name")
Toast.makeText(context, "Hello, $name!", Toast.LENGTH_SHORT).show()
} catch (e: JSONException) {
Timber.d("Unable to get user name")
}
}
val parameters = Bundle()
parameters.putString("fields", "name")
request.parameters = parameters
request.executeAsync()
这里是 java
中的代码片段
public void requestData(){
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object,GraphResponse response) {
JSONObject json = response.getJSONObject();
try {
if(json != null){
String name = user.getString("name");
Toast.makeText(context, "Welcome "+name, Toast.LENGTH_SHORT).show()
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,email,picture");
request.setParameters(parameters);
}
在我的应用程序中,可以使用 Facebook 登录,在成功登录后,我想显示一条 toast 消息,上面写着 "Welcome back, username (which is the displayName)"。我设法显示了一条消息,但没有用户名,因为我不知道如何从 Firebase 获取它并在登录后显示它。
这是处理 Facebook 登录的代码:
private void handleFacebookAccessToken(AccessToken token) {
Log.d(TAG, "handleFacebookAccessToken:" + token);
progressBar.setVisibility(View.VISIBLE);
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);
// Todo make a toast with the username
Toast.makeText(SignInActivity.this, "Welcome back", Toast.LENGTH_SHORT).show();
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
Toast.makeText(SignInActivity.this, "Error.",
Toast.LENGTH_LONG).show();
updateUI(null);
}
progressBar.setVisibility(View.INVISIBLE);
}
});
}
获得 Facebook 访问令牌后,您可以使用 GraphApi 获取用户的附加信息:
val request = GraphRequest.newMeRequest(
accessToken
) { user, _ ->
try {
val name = user.getString("name")
Toast.makeText(context, "Hello, $name!", Toast.LENGTH_SHORT).show()
} catch (e: JSONException) {
Timber.d("Unable to get user name")
}
}
val parameters = Bundle()
parameters.putString("fields", "name")
request.parameters = parameters
request.executeAsync()
这里是 java
中的代码片段public void requestData(){
GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object,GraphResponse response) {
JSONObject json = response.getJSONObject();
try {
if(json != null){
String name = user.getString("name");
Toast.makeText(context, "Welcome "+name, Toast.LENGTH_SHORT).show()
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link,email,picture");
request.setParameters(parameters);
}