简体   繁体   English

正在从数据库中删除特定用户的所有通知,此时只应删除一个

[英]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.

相关问题 当用户不喜欢评论时,该用户的所有通知都会从数据库中删除,而不仅仅是该特定通知 - When user unlikes comment all notifications of that user get deleted from database instead of only that specific notification 玩家退出游戏活动,房间应该只能由他移除,但所有玩家的房间都会被删除 - The player exits the game activity, and the room should be removed only by him, but the rooms of all players are deleted 当用户再次登录时,数据已从Firebase数据库Android中删除 - When user login again then data was removed from Firebase database Android 获取删除父级后所有已删除实体的ID? - Get ID of all deleted entities when parent is removed? 在使用Tomcat和JDBC Store的PersistentManager时,会话从数据库中过早删除 - Session being deleted prematurely from database when using Tomcat and PersistentManager using JDBC Store servlet仅显示数据库中的一个数据,而不是全部 - servlet show only one data from database not all 用户删除应用程序后如何删​​除所有文件? - How to remove all file when app is removed by user? 使用 log4j 时,程序创建多个文件但只填充一个,什么时候应该填充所有 - When using log4j, program creates mutliple files but fills only one, when should fill all 拍摄时未取下碎片 - Pieces not being removed when taken 数据库连接应该一直保持打开状态还是只在需要时才打开? - Should a database connection stay open all the time or only be opened when needed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM