0

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"
      }
    }
    
  }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 1
    If you created your database in a non-default region, it could be that you downloaded the `google-services.json` before the database was created. If that is the case, you can solve it by specifying the database URL in your code in `FirebaseDatabase.getInstance("database URL here").getReference()...` – Frank van Puffelen Jul 04 '21 at 15:22
  • Does Frank van Puffelen's suggestion solve your problem? Please respond with @. – Alex Mamo Jul 05 '21 at 09:26
  • @FrankvanPuffelen The way definitely solve the problem. Thanks for your help. – Tina Chang Jul 06 '21 at 10:04

0 Answers0