onChildAdded通话两次?

时间:2019-04-07 02:53:27

标签: android firebase firebase-realtime-database

尽管之前曾问过此问题,但未正确回答。以下代码调用createNewCommentNotification(commenterUid,postsModel);方法两次。解决办法是什么。

enter image description here

Query commentQuery = databaseReference.child("Notifications/NewComment").child(mUser.getUid());

commentQuery.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        if (dataSnapshot.exists()) {

            final String commenterUid = dataSnapshot.getKey();

            for (DataSnapshot ds : dataSnapshot.getChildren()) {

                final PostsModel postsModel = ds.getValue(PostsModel.class);
                createNewCommentNotification(commenterUid, postsModel);

            }
        }
    }

    @Override
    public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { }

    @Override
    public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) { }

    @Override
    public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) { }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) { }
});

下面是Notifications节点上的JSON数据

    {
  "NewComment" : {
    "q55o2IU76OS0ewhEW6thVALevwZ2" : {
      "OmF2nnyrgRWyQ5EtyKN5hQjPU723" : {
        "8445392926859" : {
          "customId" : "8451940719828",
          "dist" : "Jorhat",
          "imageUrl" : "https://firebasestorage.googleapis.com/v0/b/fir-first-95f01.appspot.com/o/Photos%2Fq55o2IU76OS0ewhEW6thVALevwZ2%2F8445392926859.jpg?alt=media&token=d509d929-e72f-4639-b96f-0e2923f7a2c7",
          "money" : 0,
          "newPostKey" : "8445392912509",
          "post" : "",
          "postId" : "8445392926859",
          "thumbImageUrl" : "https://firebasestorage.googleapis.com/v0/b/fir-first-95f01.appspot.com/o/PostsThumb%2Fq55o2IU76OS0ewhEW6thVALevwZ2%2F8445392926859.jpg?alt=media&token=37e136ea-5cb6-4442-a049-25fc5be1af1b",
          "time" : 1554607086944,
          "userId" : "q55o2IU76OS0ewhEW6thVALevwZ2"
        }
      }
    }
  }
}

0 个答案:

没有答案