I tried many times to insert the register data by hashmap into firebase realtime database, but only the authentication one is success. The addOnCompleteListener
behind the setValue()
didn't be trigger at all. The intent didn't start. Does anyone know how to fix with?
private void register(String user_name, String email, String txt_password, String txt_mobile, String gender) {
progressBar.setVisibility(View.VISIBLE);
firebaseAuth.createUserWithEmailAndPassword(email, txt_password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull @NotNull Task<AuthResult> task) {
if(task.isSuccessful()) {
FirebaseUser rUser = firebaseAuth.getCurrentUser();
assert rUser != null;
String userId = rUser.getUid();
databaseReference = FirebaseDatabase.getInstance().getReference().child(userId);
HashMap<String, String> map = new HashMap<>();
map.put("userId", userId);
map.put("username", user_name);
map.put("email", email);
map.put("mobile", txt_mobile);
map.put("gender", gender);
map.put("imageURL", "default");
databaseReference.push().setValue(map).addOnCompleteListener(new OnCompleteListener<Void>() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onComplete(@NonNull Task<Void> task) {
progressBar.setVisibility(View.GONE);
if (task.isSuccessful()) {
Toast.makeText(RegisterActivity.this, "註冊成功", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK| Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} else {
Toast.makeText(RegisterActivity.this, Objects.requireNonNull(task.getException()).getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
else{
progressBar.setVisibility(View.GONE);
Toast.makeText(RegisterActivity.this, Objects.requireNonNull(task.getException()).getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
My realtime database rule is
{"rules": {
"Users":{
"$user_id":{
".write":"$user_id === auth.uid && auth !== null",
".read":"$user_id === auth.uid && auth !== null"
}
}
}
}