我遇到的问题是我编写了两种不同的方法:1 用于向数据库添加通知,1 用于删除该通知。如果其他用户喜欢您的帖子、对您的帖子发表评论、喜欢您的评论等,您会收到一条通知。在这些情况下,发布该帖子的用户将收到一条通知,让他们知道您喜欢他们的评论或他们的帖子等.
问题在于通知可以很好地保存到数据库中,但是当用户不喜欢某个帖子时,不仅会删除该通知,还会删除该用户的所有通知。我不确定为什么会发生这种情况,我只是希望从数据库中删除具有特定 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) {
}
});
}
第二种方法
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();
}
这些行应该改变。因为通知 ID 永远不会为空,并且所有通知都会被删除。
if (notification.getUserId().equals(mFirebaseUser.getUid())) {
reference.child(notification.getNotificationId()).removeValue();
}
或者您可以使用其他方式保存通知 ID 并删除通知,而不检查所有通知。
添加功能:
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);
}
删除功能:
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) {
}
});
}
关于java - 特定用户的所有通知都将从数据库中删除,而实际上只应删除一条通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61461415/