Android 如何在使用Firebase登录Facebook后显示带有displayName的toast消息?

Android 如何在使用Firebase登录Facebook后显示带有displayName的toast消息?,android,firebase-authentication,facebook-authentication,android-toast,Android,Firebase Authentication,Facebook Authentication,Android Toast,在我的应用程序中,可以使用Facebook登录,成功登录后,我想显示一条toast消息,上面写着“欢迎回来,用户名(这是displayName)”。我设法显示了一条消息,但没有用户名,因为我不知道如何从Firebase获取消息并在登录后显示 以下是处理Facebook登录的代码: private void handleFacebookAccessToken(AccessToken token) { Log.d(TAG, "handleFacebookAccessToken:" +

在我的应用程序中,可以使用Facebook登录,成功登录后,我想显示一条toast消息,上面写着“欢迎回来,用户名(这是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);
                    }
                });
    }
私有无效handleFacebookAccessToken(AccessToken令牌){
Log.d(标记“handleFacebookAccessToken:”+token);
progressBar.setVisibility(View.VISIBLE);
AuthCredential credential=FacebookAuthProvider.getCredential(token.getToken());
mAuth.SIGNWITH凭证(凭证)
.addOnCompleteListener(这是新的OnCompleteListener(){
@凌驾
未完成的公共void(@NonNull任务){
if(task.issusccessful()){
//登录成功,使用登录用户的信息更新UI
Log.d(标记“signInWithCredential:success”);
FirebaseUser=mAuth.getCurrentUser();
updateUI(用户);
//Todo使用用户名敬酒
Toast.makeText(SignInActivity.this,“欢迎回来”,Toast.LENGTH_SHORT.show();
}否则{
//如果登录失败,则向用户显示消息。
w(标记“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);
}

您是否将用户名保存到firebase数据库?如果在facebook访问令牌之前保存make query并从数据库中检索读取公共操作或用户名的权限,然后获取用户名并将其传递给ast,您可以显示您的类代码,这很容易解决。