The problem that I am having is that I have written two different methods: 1 for adding a notification to the database, and 1 for removing that notification. You receive a notification is another user likes your post, comments on your post, likes your comment, etc. In those cases the user whose post it is will receive a notification letting them know that you have liked their comment, or their post, etc.
The issue is that the notifications are being saved to the database just fine, but when a user for example unlikes a post, not just that notification is deleted, but all of the notifications of that user. I am not sure why this is happening, I would just like that one notification with its specific notificationId
to be removed from the database, not all of the notifications that user has received.
PostAdapter
holder.like.setOnClickListener(v -> {
if (holder.like.getTag().equals("like")) {
FirebaseDatabase.getInstance().getReference().child("Likes").child(post.getPostid()).child(mFirebaseUser.getUid()).setValue(true);
addNotification(post.getPublisher(), post.getPostid());
} else {
FirebaseDatabase.getInstance().getReference().child("Likes").child(post.getPostid()).child(mFirebaseUser.getUid()).removeValue();
deleteNotification(post.getPublisher());
}
});
private void addNotification(final String userid, final String postid) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Notifications").child(userid);
String notificationId = reference.push().getKey();
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("userid", mFirebaseUser.getUid());
hashMap.put("comment", "liked your event");
hashMap.put("postid", postid);
hashMap.put("notificationId", notificationId);
hashMap.put("ispost", true);
if (notificationId != null)
reference.child(notificationId).setValue(hashMap);
}
private void deleteNotification(String userid) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Notifications").child(userid);
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Notification notification = snapshot.getValue(Notification.class);
if (notification != null) {
reference.child(notification.getNotificationId()).removeValue();
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
Second method
holder.like.setOnClickListener(v -> {
if (holder.like.getTag().equals("like")) {
FirebaseDatabase.getInstance().getReference().child("Likes").child(post.getPostid()).child(mFirebaseUser.getUid()).setValue(true);
addNotification(post.getPublisher(), post.getPostid());
} else {
FirebaseDatabase.getInstance().getReference().child("Likes").child(post.getPostid()).child(mFirebaseUser.getUid()).removeValue();
FirebaseDatabase.getInstance().getReference().child("Notifications").child(post.getPublisher()).child(mNotificationId).removeValue();
}
});
private void addNotification(final String userid, final String postid) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Notifications").child(userid);
mNotificationId = reference.push().getKey();
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("userid", mFirebaseUser.getUid());
hashMap.put("comment", "liked your event");
hashMap.put("postid", postid);
hashMap.put("notificationId", mNotificationId);
hashMap.put("ispost", true);
reference.child(mNotificationId).setValue(hashMap);
}
if (notification != null) {
reference.child(notification.getNotificationId()).removeValue();
}
These lines should change. Because the notification id is never null and all notifications are deleted.
if (notification.getUserId().equals(mFirebaseUser.getUid())) {
reference.child(notification.getNotificationId()).removeValue();
}
Or you can save the notification id using another way and delete the notificaition without checking all the notifications.
Add func:
private void addNotification(final String userid, final String postid)
{
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Notifications").child(userid);
mNotificationId = reference.push().getKey();
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("userid", mFirebaseUser.getUid());
hashMap.put("comment", "liked your event");
hashMap.put("postid", postid);
hashMap.put("notificationId", mNotificationId);
hashMap.put("ispost", true);
reference.child(mNotificationId).setValue(hashMap);
HashMap<String, Object> hashMap2 = new HashMap<>();
hashMap2.put("notificationId", mNotificationId);
hashMap2.put("like", true);
FirebaseDatabase.getInstance().getReference().child("Likes").child(postid).child(mFirebaseUser.getUid()).setValue(hashMap2);
}
delete func:
private void deleteNotification(String userid, final String postid)) {
DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Likes").child(post.getPostid()).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);
FirebaseDatabase.getInstance().getReference("Notifications")
.child(userid).child(notificationId).removeValue();
reference.removeValue();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.