Currently developing a Forum App where user who post question will get a notification if someone respond to them. Right now the coding has logical error where the notification does not go into the original poster attribute in the User table inside Firebase
Below is the code to add notification
private void addToHisNotification(String hisUid,String Post_Key,String notification){
String timestamp = ""+System.currentTimeMillis();
HashMap<Object, String> hashMap = new HashMap<>();
hashMap.put("timestamp",timestamp);
hashMap.put("Postkey",Post_Key);
hashMap.put("uid",hisUid);
hashMap.put("notification",notification);
hashMap.put("sUid",current_user_id);
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Users");
ref.child(hisUid).child("Notifications").child(timestamp).setValue(hashMap)
.addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
And this is the function to add Answer and will call function Notification
PostAnsButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Userref.child(current_user_id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
String userName = dataSnapshot.child("Username").getValue().toString();
String profileImg = dataSnapshot.child("profileImage").getValue().toString();
String department = dataSnapshot.child("Department").getValue().toString();
ValidateAnswer(userName,profileImg, department);
addToHisNotification("" + hisUid,"" + Post_Key, "Commented on your post");
AnswerInputText.setText("");
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
});
The Null in the image is supposed to be the original poster UID but instead it becomes null. How do I solve this so that the Null becomes the UID of the OP?