Reputation: 458
I'm trying to add some contact data in the firebase realtime database separately from the user data. This is my tree structure:
What I am trying to do is, get the name
value of the user (e.g. UID-1
) and in the Contact
child, create a new child of that same user and write the name
value as well as two other values (contatcMethod
and phoneValue
).
This is my code so far (I have a RadioGroup
for the contactMethod
):
public class ContactDetails extends AppCompatActivity {
RadioGroup radioGroup;
RadioButton radioButton;
private EditText phoneNumber;
private FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
mAuth = FirebaseAuth.getInstance();
radioGroup = findViewById(R.id.radioGroup);
phoneNumber = findViewById(R.id.phoneNumber);
Button submit = findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int radioId = radioGroup.getCheckedRadioButtonId();
radioButton = findViewById(radioId);
contact();
}
});
}
private void contact() {
FirebaseUser user = mAuth.getCurrentUser();
String userID = user.getUid();
final String phoneValue = phoneNumber.getText().toString().trim();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users");
ref.child(userID).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
final String name = dataSnapshot.child("name").getValue().toString();
UserContact userContact = new UserContact(
contactMethod,
phoneValue,
name
);
FirebaseDatabase.getInstance().getReference("Contact")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(userContact).addOnCompleteListener(new OnCompleteListener < Void > () {
@Override
public void onComplete(@NonNull Task < Void > task) {
if (task.isSuccessful()) {
Toast.makeText(ContactDetails.this, "Saved Successfully", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(ContactDetails.this, "Not successful", Toast.LENGTH_LONG).show();
}
}
});
}
});
}
}
And my UserContact
java is:
package com.rimikri.smartpedi;
public class UserContact {
public String contactMethod, phoneValue, name;
public UserContact() {
}
public UserContact(String contactMethod, String phoneValue, String name) {
this.contactMethod = contactMethod;
this.phoneValue = phoneValue;
this.name = name;
}
}
But I'm not being successful in writing any data in the database with this code. It's returning the "Not successful" toast. I'd very much appreciate to learn what I'm doing wrong.
The error I've got is:
DatabaseError: Permission denied
Upvotes: 0
Views: 199
Reputation: 613
Seems like it might be a permission issue because otherwise, the code should work. Try the following to update the permission of the database read and write.
{
"rules": {
"Users": {
"$uid": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid"
}
},
"Contact": {
"$uid": {
".read": "$uid === auth.uid",
".write": "$uid === auth.uid"
}
}
}
}
Upvotes: 1