【问题标题】:How to delete a certain data in the Firebase Database?如何删除 Firebase 数据库中的某些数据?
【发布时间】:2020-12-29 09:05:34
【问题描述】:

我正在开发一个社交网络应用程序,用户可以在其中关注其他用户并喜欢他们的帖子,cmets ... 每当某个用户关注某人时,它会显示在另一个用户帐户的通知片段中,以通知他他有一个新的关注者。 问题是当用户点击取消关注时我无法删除通知。这是我尝试过的:

 if ( holder.btn_follow.getText().toString().equals("follow"))
 {
addNotifications(user.getId());
}
 else{
 FirebaseDatabase.getInstance().getReference("Notifications").child(user.getId()).removeValue();
   }

这是我添加通知的方式:

 private void addNotifications(String userid)
    {
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Notifications").child(userid);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("userid", firebaseUser.getUid());
        hashMap.put("text"," started following you");
        hashMap.put("postid","");
        hashMap.put("ispost",false);
        reference.push().setValue(hashMap);
    }

我的代码的问题在于,每当用户取消关注某人时,来自该用户的所有通知都会被删除,包括他的点赞和 cmets。我只想删除“开始关注你”。 这是它在 Firebase 数据库中的外观。

【问题讨论】:

标签: android firebase firebase-realtime-database


【解决方案1】:

使用 Firebase 查询引用Firebase Docs,会是这样的

Query queryRef = mReference.child("Notifications").child(userId).orderByChild("text").equalTo("start following you");

queryRef.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot snapshot, String previousChild) {
      // snapshot.getRef().setValue(null);
      snapshot.getRef().remove();
    }
});

【讨论】:

  • 看起来不错,我认为它会起作用。但我没有notificationId。我不知道如何访问要删除的数据。这就是问题
  • 在数据库中,好像有一个notificationid,但我不知道为什么,因为我没有在我的代码中分配它
  • 我更新了。它甚至应该以这种方式工作
猜你喜欢
  • 2021-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-30
  • 1970-01-01
相关资源
最近更新 更多