Firebase + Android - Notifications ChildAdded - How to get only New Childs?

前端 未结 3 847
既然无缘
既然无缘 2021-02-03 13:46

I\'m trying to setup an Android Service in my app that listens for new Childs in a Firebase Ref, and throws a Notification when that happens.

I\'m having issues because

3条回答
  •  自闭症患者
    2021-02-03 13:50

    For me the logic was is to have value -"status" for example- which needs to be validated before deciding whether it is really new or was an old record then I set the "status" to a different value so I don't get it next time:

    @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String previousChildKey) {
    
                    if(dataSnapshot.hasChildren()) {
    
                        String Id = (String) dataSnapshot.child("user_id").getValue();
                        String status = (String) dataSnapshot.child("status").getValue();
    
                        if (Id != null && Id.equals(storedId) && status != null && status.equals("created")) {
                            Log.d("INCOMING_REQUEST", "This is for you!");
                            sessionsRef.child(dataSnapshot.getKey()).child("status").setValue("received");
                        }
    
                    }
    
    
            }
    

提交回复
热议问题