java - 正在从数据库中删除特定用户的所有通知,此时只应删除一个
问题描述
我遇到的问题是我编写了两种不同的方法:1 用于向数据库添加通知,1 用于删除该通知。您收到通知是其他用户喜欢您的帖子、对您的帖子发表评论、喜欢您的评论等。在这种情况下,发布该帖子的用户将收到通知,让他们知道您喜欢他们的评论或他们的帖子等.
问题是通知被保存到数据库中就好了,但是当一个用户不喜欢一个帖子时,不仅仅是那个通知被删除,而是那个用户的所有通知。我不确定为什么会发生这种情况,我只想notificationId
从数据库中删除一个通知,而不是用户收到的所有通知。
后适配器
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) {
}
});
}
推荐阅读
- python - Jinja2 for PyCharm?
- flutter - 是否可以为使用 CupertinoApp() 的应用程序使用 Get 包?
- r - 使用R中的条件对象将“立即”设置为“真”?
- python - 如何拆分成二维数组
- graphql - 通过 URI 查询的 WPGraphQL 对自定义帖子类型返回 null
- php - 在php中获取二叉树中的最后一个左孩子
- python - Evenly divide a list of integers into a equal or larger list of integers with id's
- scala - 无法在 Scala 中编译和运行多个文件
- r - 如何解决此错误 - 缺少 $ 插入?
- mfc - MFC 对话框 - 如何调整和重新设置客户端“视图”矩形?