首页 > 解决方案 > 正在从数据库中删除特定用户的所有通知,此时只应删除一个

问题描述

我遇到的问题是我编写了两种不同的方法: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);
    }

标签: javaandroidfirebase

解决方案


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) {

 }
 });
 }

推荐阅读