我正在使用firebase创建一个用于登录屏幕的项目。我试图用下面的代码更新用户的displayName,但它没有更新显示名称。帮帮我
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName(name)
.build();
user.updateProfile(profileUpdates).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Toast.makeText(VerifyUser.this,"Name updated successfully",Toast.LENGTH_LONG).show();
startActivity(new Intent(VerifyUser.this,MainActivity.class));
}else
Toast.makeText(VerifyUser.this,"Name update Failed",Toast.LENGTH_LONG).show();
}
});
答案 0 :(得分:0)
您需要在登录或验证电子邮件和密码后立即更新用户配置文件。
我知道我迟到了,但为了后代,要更新 firebase 上的用户属性,他们需要先登录。
所以像:
mAuth.signInWithCredential(credential)
.addOnCompleteListener(LordRegister.this,
new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
//verification successful we will start the profile activity
FirebaseUser user = task.getResult().getUser();
String name = nameInput.getText().toString().trim();
//user.updateProfile()
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName(name)
.build();
user.updateProfile(profileUpdates).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Intent intent = new Intent(LordRegister.this, LordHome.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}else
Toast.makeText(LordRegister.this,"Name update Failed, try again",Toast.LENGTH_LONG).show();
}
});
} else {
//verification unsuccessful.. display an error message
String message = "Somthing is wrong, we will fix it soon...";
if (task.getException() instanceof FirebaseAuthInvalidCredentialsException) {
message = "Invalid code entered...";
}
Toast.makeText(LordRegister.this,message,Toast.LENGTH_SHORT).show();
}
}
});
}
像上面一样,我使用用户电话号码登录或先验证用户,然后更新名称或任何其他属性。