首先让我说没有必要降级Gradle文件或注销并像其他人所说的那样多次登录用户只是为了显示用户名。我以不同的方式解决了这个问题,同时仍然保留了最新的 Gradle 文件,而不必多次注销用户。 getDisplayName() 的问题是一个非常大的问题,因此我希望尽可能为 Firebase 的未来用户提供描述性,以免他们头疼。
以下是解决方案的详细信息:
解决方案 1:
对于通过多个提供商(例如 Facebook、Google 等)和/或他们在注册时创建的电子邮件/密码进行身份验证(登录)的用户:
您要确保的第一件事是,当您有用户第一次注册应用程序时,您当然会将他们的姓名存储到数据库中,并使用他们的唯一 ID。可能看起来像这样:
// Method signature. Write current user's data to database
private void writeNewUser(String userId, String name, String email) {
DatabaseReference current_user_database = mDatabaseRef.child(userId);
current_user_database.child("username").setValue(name);
// Email here is not mandatory for the solution. Just here for clarity of the
// method signature
current_user_database.child("email").setValue(email);
}
用户名存储在您的 Firebase 数据库中并且您让他们登录您的应用后,您可以获取他们的用户名,如下所示:
// Method that displays the user's username
private void showUserName() {
// Get instance of the current user signed-in
mFirebaseUser = mAuth.getCurrentUser();
// Check if user using sign-in provider account is currently signed in
if (mFirebaseUser != null) {
// Get the profile information retrieved from the sign-in provider(s) linked to a user
for (UserInfo profile : mFirebaseUser.getProviderData()) {
// Name of the provider service the user signed in with (Facebook, Google, Twitter, Firebase, etc.)
String name = profile.getDisplayName();
// If displayName() is null this means that the user signed in using the email and password they created. This is the null issue we shouldn't get that we are gonna be solving below.
if(name == null){
mDatabaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get the name the user signed-up with using dataSnapshot
String nameOfCurrentUser = (String) dataSnapshot.child("name").getValue();
// Set username we retrieved using dataSnapshot to the view
mTestUsernameTextView.setTitle(nameOfCurrentUser);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
// If the name is not null that means the user signed in with a social Auth provider such as Facebook, Twitter, Google etc.
if(name != null) {
// Set the TextView (or whatever view you use) to the user's name.
mTestUsernameTextView.setText(name);
}
} // End for
} // End if
}
就是这样。现在您只需调用该方法 showUserName() 或在 onCreate 中调用您的任何方法,希望这会有所帮助。
解决方案 2:
对于仅使用社交媒体服务提供商(例如 Facebook、Twitter、Google 或 Firebase 允许的任何其他选项)登录您的应用的用户:
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
for (UserInfo profile : user.getProviderData()) {
// Id of the provider (ex: google.com)
String providerId = profile.getProviderId();
// UID specific to the provider
String uid = profile.getUid();
// Name, email address, and profile photo Url
String name = profile.getDisplayName();
String email = profile.getEmail();
Uri photoUrl = profile.getPhotoUrl();
};
}
解决方案 2 就是这样,只需按照 Firebase 上的指南进行操作,就可以了。
如果你好奇,这里有一个链接:
Get a user's profile information
我真的希望这可以帮助任何在getDisplayName() 问题上苦苦挣扎的人。
结论:
如果您的应用只有 Facebook、Twitter、Google 或 Firebase 提供的任何其他社交媒体登录选项,那么只需对当前登录的用户调用 getDisplayName()method 就足以显示他们的用户名。
否则,如果您的应用允许用户使用他们创建的电子邮件/密码登录,请确保您在注册时获得了他们的姓名/用户名,以便您以后可以使用它来显示它。