通知节点在图像视图点击时没有按预期删除通知及其数据

huangapple 未分类评论48阅读模式
英文:

Notification node isn't deleting notification and its data on ImageView click when it should be

问题

我在Activity中有两个方法,一个用于给评论添加喜欢(点击ImageView),并将通知发送到Firebase,同时该帖子的用户会收到通知;另一个方法是将喜欢移除(再次点击ImageView)。问题是,通知和评论都会显示出来,并保存到数据库中,但是当我再次点击以移除“喜欢”时,评论会从数据库中移除,但通知却没有被移除。

有人可以帮我检查一下这个方法的逻辑吗?我可能做错了什么,因为通知没有被移除,只有评论被移除了。

private void likeComment(String commentId, String postId, String userId) {
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("Comments Liked").child(commentId).child(mFirebaseUser.getUid());

    mNotificationId = reference.push().getKey();

    HashMap<String, Object> hashMap = new HashMap<>();
    hashMap.put("notificationId", mNotificationId);
    hashMap.put("commentid", commentId);
    hashMap.put("userid", mFirebaseUser.getUid());

    reference.setValue(hashMap);

    HashMap<String, Object> hashMap1 = new HashMap<>();
    hashMap1.put("userid", mFirebaseUser.getUid());
    hashMap1.put("comment", "liked your comment");
    hashMap1.put("postid", postId);
    hashMap1.put("ispost", true);
    hashMap1.put("notificationId", mNotificationId);

    FirebaseDatabase.getInstance().getReference("Notifications").child(userId).child(mNotificationId).setValue(hashMap1);
}

private void removeLike(String userId, String commentId) {
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Comments Liked").child(commentId).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);
                if (notificationId != null) {
                    FirebaseDatabase.getInstance().getReference("Notifications").child(userId).child(notificationId).removeValue();
                    reference.removeValue();
                }
            }
        }

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

        }
    });
}
英文:

I've got two methods in my Activity, one to add a like to a comment (clicking on ImageView) and notification to Firebase and also the user whose post it is gets a notification, and then another to remove it (clicking on ImageView again). Problem is that notification and comment both show up, and get saved to database, but then when I click again to remove the "like" the comment get removed from the database, but the notification doesn't.

Can someone help me out with the logic of this method. Something I am doing wrong because notification doesn't get removed only comment.

 private void likeComment(String commentId, String postId, String userId) {
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child(&quot;Comments Liked&quot;).child(commentId).child(mFirebaseUser.getUid());

        mNotificationId = reference.push().getKey();

        HashMap&lt;String, Object&gt; hashMap = new HashMap&lt;&gt;();
        hashMap.put(&quot;notificationId&quot;, mNotificationId);
        hashMap.put(&quot;commentid&quot;, commentId);
        hashMap.put(&quot;userid&quot;, mFirebaseUser.getUid());

        reference.setValue(hashMap);

        HashMap&lt;String, Object&gt; hashMap1 = new HashMap&lt;&gt;();
        hashMap1.put(&quot;userid&quot;, mFirebaseUser.getUid());
        hashMap1.put(&quot;comment&quot;, &quot;liked your comment&quot;);
        hashMap1.put(&quot;postid&quot;, postId);
        hashMap1.put(&quot;ispost&quot;, true);
        hashMap1.put(&quot;notificationId&quot;, mNotificationId);

        FirebaseDatabase.getInstance().getReference(&quot;Notifications&quot;).child(userId).child(mNotificationId).setValue(hashMap1);
    }

    private void removeLike(String userId, String commentId) {
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference(&quot;Comments Liked&quot;).child(commentId).child(mFirebaseUser.getUid());
        reference.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                if (dataSnapshot.exists()) {
                    String notificationId = dataSnapshot.child(&quot;notificationId&quot;).getValue(String.class);
                    if (notificationId != null) {
                        FirebaseDatabase.getInstance().getReference(&quot;Notifications&quot;).child(userId).child(notificationId).removeValue();
                        reference.removeValue();
                    }
                }
            }

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

            }
        });
    }

huangapple
  • 本文由 发表于 2020年5月5日 21:09:17
  • 转载请务必保留本文链接:https://java.coder-hub.com/61613952.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定