Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve ServerValue.Timestamp from Firebase in Android app, when data is sent

I would like to know how to use Firebase's ServerValue.TIMESTAMP method, when I want to create a timestamp at the Firebase server, and then retrieve it to the local client.

In Firebase guides, only javascript has a more detailed description of this case, but I'm having a hard time figureing how to translate this in to my Android appliction.

Thanks in advance!

like image 471
user3537089 Avatar asked Sep 09 '14 12:09

user3537089


1 Answers

Firebase.ServerValue.TIMESTAMP is set as a Map (containing {.sv: "timestamp"}) which tells Firebase to populate that field with the server's time. When that data is read back, it is the actual unix time stamp which is a Long.

Something like this will work:

Firebase ref = new Firebase("https://YOUR-FIREBASE.firebaseio.com");    

ref.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        Long timestamp = (Long) snapshot.getValue();
        System.out.println(timestamp);
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

ref.setValue(ServerValue.TIMESTAMP);

For another example, you can see my answer to this question: Android chat crashes on DataSnapshot.getValue() for timestamp

like image 102
Ossama Avatar answered Sep 19 '22 10:09

Ossama