Home > Software design >  Notification From Firebace get last value added
Notification From Firebace get last value added

Time:02-28

How can I receive the last added value in firebase?

Notification From Firebace get last value added

This code count all the value from list. When I add new value, i went to get same value not all value.

send data

private void addNotification(String userid, String postid){

    DatabaseReference reference = FirebaseDatabase.getInstance( )   
        . getReference("Notifications").child(userid);
    
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("userid", firebaseUser.getUid());
        hashMap.put("text", "liked your post");
        hashMap.put("postid", postid);
        hashMap.put("ispost", true);

        reference.push().setValue(hashMap);
    }

recive data

final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query lastQuery = reference.child("Notifications").child(firebaseUser.getUid());
lastQuery.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        notification_badge.setText(dataSnapshot.getChildrenCount()   "");
        notification_badge.setVisibility(View.VISIBLE);
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {
        // Handle possible errors.
    }
});

CodePudding user response:

Firebase synchronizes the state at the path that you read from (Notifications). If you want to detect what has changed, you have a few options:

  1. Delete the data from the path once you've read and processed it, so that it does not exist the next time you read from that path.
  2. Keep a timestamp in each child/notification node, track in the application code when you last read the path, and then use a query to only get new nodes: lastQuery.orderByChild("timestamp").startAt(lastTimeYouChecked).

Also see:

  • Related