如何在Firebase实时数据库中检查现有的用户名

10 浏览
0 Comments

如何在Firebase实时数据库中检查现有的用户名

我如何在Firebase实时数据库中检查现有的用户名?

比如...我希望在填写下面的EditText“username”时,通知用户他/她选择的用户名已被其他人使用。所以他们需要选择其他用户名。我该如何做?请帮帮我..我被卡住了..

private static final String TAG = "CompleteTheRegistratio";

EditText firstname,lastname,mobno,username,useremail,useraddress;

Button submitUserDetailsButton;

FirebaseAuth firebaseAuth;

String fname,lname,mno,uname,uemail,uadd,fcm;

TextView userFcm;

@Override

protected void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_complete_the_registration);

ActionBar actionBar = getSupportActionBar();

assert actionBar!= null;

actionBar.hide();

firstname = findViewById(R.id.etFirstName);

lastname = findViewById(R.id.etLastName);

mobno = findViewById(R.id.etCellphone);

username = findViewById(R.id.etUserName);

useremail = findViewById(R.id.etEmail);

useraddress = findViewById(R.id.etAddress);

userFcm = findViewById(R.id.tvUserFcm);

submitUserDetailsButton = findViewById(R.id.btnSubmitUserDetails);

firebaseAuth = FirebaseAuth.getInstance();

submitUserDetailsButton.setOnClickListener(new View.OnClickListener(){

@Override

public void onClick(View view){

if(firstname.getText().toString()。isEmpty()|| lastname.getText().toString()。isEmpty()|| mobno.getText().toString()。length()<10 || username.getText().toString()。isEmpty()){

Toast.makeText(CompleteTheRegistrationActivity.this,“请填写所有必填字段”,Toast.LENGTH_SHORT)。show();

} else {

sendUserData();

Toast.makeText(CompleteTheRegistrationActivity.this,“注册成功!”,Toast.LENGTH_LONG)。show();

startActivity(new Intent(CompleteTheRegistrationActivity.this,DashboardActivity.class));

finish();

}

}

});

//生成FCM令牌并将其存储在TextView中:

FirebaseMessaging.getInstance()。getToken()

.addOnCompleteListener(new OnCompleteListener(){

@Override

public void onComplete(@NonNull Task任务){

if(!task.isSuccessful()){

Log.w(TAG,“获取FCM注册令牌失败”,任务.getException());

return;

}

//获取新的FCM注册令牌

String token = task.getResult();

//记录和提示

String msg = getString(R.string.msg_token_fmt,token);

Log.d(TAG,msg);

Toast.makeText(CompleteTheRegistrationActivity.this,“令牌已生成!”,Toast.LENGTH_SHORT)。show();

userFcm.setText(msg);

}

});

}

private void sendUserData(){

//最后,将用户数据与fcm令牌一起发送到Realtime Database与myRef引用。

fname = firstname.getText().toString();

lname = lastname.getText().toString();

mno = mobno.getText().toString();

uname = username.getText().toString();

uemail = useremail.getText().toString();

uadd = useraddress.getText().toString();

fcm = userFcm.getText().toString();

FirebaseDatabase firebaseDatabase = FirebaseDatabase.getInstance();

DatabaseReference myRef = firebaseDatabase.getReference(firebaseAuth.getUid());

UserProfile userProfile = new UserProfile(fname,lname,mno,uname,uemail,uadd,fcm);

myRef.setValue(userProfile);

//将用户的用户名发送到Realtime Database中的其Uids中:

FirebaseDatabase firebaseDatabase1 = FirebaseDatabase.getInstance();

DatabaseReference myRef1 = firebaseDatabase1.getReference("users");

myRef1.child(firebaseAuth.getUid()).setValue(uname);

}

下面是我的Firebase结构:

enter image description here

0