文档在消防商店snapShotListener中添加了多次

时间:2018-11-11 21:04:57

标签: java android firebase google-cloud-firestore event-listener

添加或修改孩子时,我正在使用侦听器显示通知。当添加孩子时,通知有时会出现一次。但是在其他时候,通知出现两次而不是一次。当我调试代码时,我发现鳕鱼在“ case ADDED”中发现了两次,而不是两次,但是我不知道为什么。 这是我的代码:

    EventListener generalSnapShotListener = new EventListener<QuerySnapshot>() {
        @Override
        public void onEvent(@Nullable QuerySnapshot snapshots,
                            @Nullable FirebaseFirestoreException e) {
            if (e != null) {
                Log.w("TAG, listen:error", e);
                return;
            }

            for (DocumentChange dc : snapshots.getDocumentChanges()) {
                DocumentSnapshot challengeDocument = dc.getDocument();
                switch (dc.getType()) {
                    case ADDED:{
                        String challengeState = challengeDocument.getString("state");
                        String player1Uid = challengeDocument.getString("player1Uid");
                        final String player1Name = challengeDocument.getString("player1Name");
                        final String subject = adjustSubject(challengeDocument.getString("subject"));
                        String challengeId = challengeDocument.getId();

                        currentPlayer = getCurrentPlayer(player1Uid);
                        Log.v("Logging2", "onChildAdded");
                        if (challengeState.equals(uncompletedChallengeText) && currentPlayer == 2 && !challengeDocument.getId().equals(onChildAddedPreviusKey) && !challengeDocument.getId().equals("questionsList") && challengeDocument.exists()) {
                            localFireStoreChallenges.document(challengeId).update("player2notified", true);
                            showNotification("لديك تحدى جديد", "تم تحديك فى " + subject + " بواسطة " + player1Name);
                        }
                        break;
                    }
                    case MODIFIED:
                        String challengeState = challengeDocument.getString("state");
                        String player1Uid = challengeDocument.getString("player1Uid");
                        String player2Name = challengeDocument.getString("player2Name");
                        String subject = adjustSubject(challengeDocument.getString("subject"));

                        String state = currentUserState(challengeDocument);

                        String challengeId = challengeDocument.getId();
                        currentPlayer = getCurrentPlayer(player1Uid);
                        if (currentPlayer == 1 && (challengeState.equals(completedChallengeText) || challengeState.equals(refusedChallengeText)) && !challengeDocument.getId().equals(onChildChangedpreviusKey) && !challengeDocument.getId().equals("questionsList")) {
                            showNotification("لديك تحدى مكتمل جديد",  "لقد " + state +
                                            " تحديك ضد " + player2Name
                                    /*+ " فى مادة " + subject*/);//TODO : add this
                            localFireStoreChallenges.document(challengeId).update("player1notified", true);
                            onChildChangedpreviusKey = challengeDocument.getId();
                        }
                    case REMOVED:
                }
            }
        }
    };

    //this gives the challenges that the current user has started
    Log.v("notificationsDebug", "localCurrentUserUid is : " + localCurrentUserUid);
     localFireStoreChallenges.whereEqualTo("player1notified",localCurrentUserUid + "false").addSnapshotListener(generalSnapShotListener);
    //this code gives data where current user is player 2
    localFireStoreChallenges.whereEqualTo("player2notified", localCurrentUserUid + "false").addSnapshotListener(generalSnapShotListener);

0 个答案:

没有答案