Firebase UserProfileChangeRequest 不起作用

Nir*_*rel 5 android firebase firebase-authentication

我正在尝试创建一个个人资料活动,用户可以在其中更改这些个人资料图片和显示名称,我正在尝试更新用户照片或用户名,已调用 CompleteListener,task.isSuccessful = true 但已完成,为什么?

更新名称的函数:

FirebaseUser mFirebaseUser = FirebaseAuth.getInstance().getCurrentUser();
final String newName;
newName = input.getText().toString();
UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
.setDisplayName(newName)
.build();
mFirebaseUser.updateProfile(profileUpdates)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
DatabaseReference mFirebaseDatabaseReference = FirebaseDatabase.getInstance().getReference().child("users");
   mFirebaseDatabaseReference.child(mFirebaseUser.getUid()).child("DisplayName").setValue(newName);
updateUI();
Toast.makeText(ProfileActivity.this, "User display name updated.", Toast.LENGTH_SHORT).show();
} else
Toast.makeText(ProfileActivity.this, "Error while updating display name.", Toast.LENGTH_SHORT).show();
}
});
Run Code Online (Sandbox Code Playgroud)

当我尝试更新我刚刚上传到 Firebase 存储的个人资料图片时也是如此...

和想法?

编辑:

有时用户名真的会更新,我想更新需要 10 多分钟,为什么?

ssc*_*itz 3

我遇到了类似的问题,直到用户重新进行身份验证后,用户信息才会更新。我通过将此信息保存在我的 firebase 数据库中解决了这个问题。对我来说这是有道理的,因为我希望用户无论如何都能够获取有关其他用户的基本信息。

我的代码最终看起来像这样。创建或修改帐户时,我调用“users/{uid}”端点并更新那里的对象。从这里我使用GreenRobot EventBus将新的 User 对象发送给任何订阅者,以便它可以在屏幕上更新。

private FirebaseUser firebaseUser;

public void createUser(String email, String password, final User user, Activity activity, final View view) {
    FirebaseAuth.getInstance().createUserWithEmailAndPassword(email, password)
        .addOnCompleteListener(activity, new OnCompleteListener<AuthResult>() {
            @Override
            public void onComplete(@NonNull Task<AuthResult> task) {
                Log.d(TAG, "createUserWithEmail:onComplete:" + task.isSuccessful());

                // If sign in fails, display a messsage to the user. If sign in successful
                // the auth state listener will be notified and logic to handle
                // signed in user can be handled in the listener
                if (!task.isSuccessful()) {
                    Snackbar.make(view, task.getException().getLocalizedMessage(), Snackbar.LENGTH_SHORT).show();
                } else {
                    firebaseUser = task.getResult().getUser();

                    UserProfileChangeRequest profileUpdates = new UserProfileChangeRequest.Builder()
                        .setDisplayName(user.displayName)
                        .build();
                    firebaseUser.updateProfile(profileUpdates);
                    updateDatabase(user);

                    EventBus.getDefault().post(new LoginEvent());
                }
            }
        });
}

public boolean updateDatabase(User user) {
    if (firebaseUser == null) {
        Log.e(TAG, "updateDatabase:no currentUser");
        return false;
    }

    return userReference.setValue(user).isSuccessful();
}
Run Code Online (Sandbox Code Playgroud)

数据库观察程序的设置是这样完成的。请注意,您需要确保在用户注销时删除侦听器,并在用户登录时添加新侦听器。

protected void setupDatabaseWatcher() {
    String uid = firebaseUser.getUid();

    userReference = FirebaseDatabase.getInstance().getReference("users/" + uid);
    userReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // This method is called once with the initial value and again
            // whenever data at this location is updated.
            User user = dataSnapshot.getValue(User.class);
            Log.d(TAG, "Value is: " + user);

            EventBus.getDefault().post(new UserUpdateEvent(user));
        }

        @Override
        public void onCancelled(DatabaseError error) {
            // Failed to read value
            Log.w(TAG, "Failed to read value.", error.toException());
        }
    });
}
Run Code Online (Sandbox Code Playgroud)