Firebase UserProfileChangeRequest isn't working

冷暖自知 提交于 2019-12-12 12:11:32

问题


I'm trying to create an profile activity, where users can change those Profile picture and Display name, I'm trying to update user photo or user name, CompleteListener called, task.isSuccessful = true but nathing done, why?

Function to update name:

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();
}
});

Same when i'm trying to update Profile picture that I just uploaded to Firebase Storage...

And idea?

EDIT:

Sometimes the username really get updated, I think it's take like more then 10 minutes to update, why?


回答1:


I have had a similar problem where the User information was not updating until the User re-authenticated. I resolved it by also saving this information in my firebase database. For me this made sense, as I wanted Users to be able to get basic information about other Users anyway.

My code ended up looking something like this. When the account is created, or modified, I made a call to the "users/{uid}" endpoint and updated the object there. From here I used the GreenRobot EventBus to send my new User object to whoever was subscribed so that it would be updated on the screen.

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();
}

The setup of the database watcher was done something like this. Note that you need to make sure that you remove the listener when the User logs out and add a new one when the User logs in.

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());
        }
    });
}


来源:https://stackoverflow.com/questions/40589029/firebase-userprofilechangerequest-isnt-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!