Reputation: 298
Following is screenshot of my Firebase realtime database. How can I notify a user whenever the item worker_id
is changed?
I've tried the following code, but it notifies about each type of data change. I want notification specific to change in worker_id
:
private void CheckDataChange(){
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("posts");
Query query = myRef.orderByChild("workType");
ValueEventListener valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d("Firebase","onDatachange");
for(DataSnapshot ds : dataSnapshot.getChildren()) {
try {
if(!ds.child("worker_id").getValue(String.class).equals("0")) {
Toast.makeText(MainActivity.this, "Request accepted", Toast.LENGTH_SHORT).show();
}
}catch (Exception ex){
Log.e("Firebase",ex.getMessage()+ex.getCause());
}
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("Firebase", databaseError.getMessage());
}
};
query.addValueEventListener(valueEventListener);
}
Upvotes: 0
Views: 113
Reputation: 598817
There is nothing built into Firebase to raise an event only when the worker_id
in your current structure changes.
The most common way to implement something like that would be to create a map mapping ds.getKey()
to the value of worker_id
when onDataChange
is first called, and then comparing the value you get against that for any updates.
Upvotes: 1