英文:
Notification node isn't deleting notification and its data on ImageView click when it should be
问题
我在Activity中有两个方法,一个用于给评论添加喜欢(点击ImageView
),并将通知发送到Firebase,同时该帖子的用户会收到通知;另一个方法是将喜欢移除(再次点击ImageView
)。问题是,通知和评论都会显示出来,并保存到数据库中,但是当我再次点击以移除“喜欢”时,评论会从数据库中移除,但通知却没有被移除。
有人可以帮我检查一下这个方法的逻辑吗?我可能做错了什么,因为通知没有被移除,只有评论被移除了。
private void likeComment(String commentId, String postId, String userId) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Comments Liked").child(commentId).child(mFirebaseUser.getUid());
mNotificationId = reference.push().getKey();
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("notificationId", mNotificationId);
hashMap.put("commentid", commentId);
hashMap.put("userid", mFirebaseUser.getUid());
reference.setValue(hashMap);
HashMap<String, Object> hashMap1 = new HashMap<>();
hashMap1.put("userid", mFirebaseUser.getUid());
hashMap1.put("comment", "liked your comment");
hashMap1.put("postid", postId);
hashMap1.put("ispost", true);
hashMap1.put("notificationId", mNotificationId);
FirebaseDatabase.getInstance().getReference("Notifications").child(userId).child(mNotificationId).setValue(hashMap1);
}
private void removeLike(String userId, String commentId) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Comments Liked").child(commentId).child(mFirebaseUser.getUid());
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
String notificationId = dataSnapshot.child("notificationId").getValue(String.class);
if (notificationId != null) {
FirebaseDatabase.getInstance().getReference("Notifications").child(userId).child(notificationId).removeValue();
reference.removeValue();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
英文:
I've got two methods in my Activity, one to add a like to a comment (clicking on ImageView
) and notification to Firebase and also the user whose post it is gets a notification, and then another to remove it (clicking on ImageView
again). Problem is that notification and comment both show up, and get saved to database, but then when I click again to remove the "like" the comment get removed from the database, but the notification doesn't.
Can someone help me out with the logic of this method. Something I am doing wrong because notification doesn't get removed only comment.
private void likeComment(String commentId, String postId, String userId) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Comments Liked").child(commentId).child(mFirebaseUser.getUid());
mNotificationId = reference.push().getKey();
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("notificationId", mNotificationId);
hashMap.put("commentid", commentId);
hashMap.put("userid", mFirebaseUser.getUid());
reference.setValue(hashMap);
HashMap<String, Object> hashMap1 = new HashMap<>();
hashMap1.put("userid", mFirebaseUser.getUid());
hashMap1.put("comment", "liked your comment");
hashMap1.put("postid", postId);
hashMap1.put("ispost", true);
hashMap1.put("notificationId", mNotificationId);
FirebaseDatabase.getInstance().getReference("Notifications").child(userId).child(mNotificationId).setValue(hashMap1);
}
private void removeLike(String userId, String commentId) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Comments Liked").child(commentId).child(mFirebaseUser.getUid());
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
String notificationId = dataSnapshot.child("notificationId").getValue(String.class);
if (notificationId != null) {
FirebaseDatabase.getInstance().getReference("Notifications").child(userId).child(notificationId).removeValue();
reference.removeValue();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
专注分享java语言的经验与见解,让所有开发者获益!
评论