[英]All notifications for specific user are being deleted from database, when only one should be removed
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.我遇到的问题是我编写了两种不同的方法:1 用于向数据库添加通知,1 用于删除该通知。 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.我不确定为什么会发生这种情况,我只想从数据库中删除一个带有其特定
notificationId
的通知,而不是用户收到的所有通知。
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.
因为通知 id 永远不会是 null 并且所有通知都被删除。
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.或者您可以使用其他方式保存通知 ID,然后删除通知而不检查所有通知。
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) {
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.