簡體   English   中英

正在從數據庫中刪除特定用戶的所有通知,此時只應刪除一個

[英]All notifications for specific user are being deleted from database, when only one should be removed

我遇到的問題是我編寫了兩種不同的方法: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);
    }
if (notification != null) {
     reference.child(notification.getNotificationId()).removeValue();
}

這些行應該改變。 因為通知 id 永遠不會是 null 並且所有通知都被刪除。

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

 }
 });
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM