Retrieve Servervalue.timestamp From Firebase In Android App, When Data Is Sent
Solution 1:
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
publicvoidonDataChange(DataSnapshot snapshot) {
Long timestamp = (Long) snapshot.getValue();
System.out.println(timestamp);
}
@Override
publicvoidonCancelled(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
Solution 2:
I know this question already been answered, but I wanted to share my version of the solution.
When I use the server's timestamp, i usually need to use it more than once, i.e I have some kind of startTime
and endTime
which both depend on the server's time, where startTime
is NOW
and endTime
is X seconds / minutes / hours after startTime
, so to spare the multiple requests to the server I save the server's time in a root child called serverTime
in the database, and I use it to set all dependent values.
Another thing, because of the way Firebase works with the ServerValue.Timestamp
, eventually it fires 2 events (added and changed), the first one with local timestamp and the second with the actual server's timestamp. So to overcome the issue of not receiving the correct time I added a simple OnCompleteListener
.
A short example code:
import android.support.annotation.NonNull;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.ServerValue;
import com.google.firebase.database.ValueEventListener;
publicclassServerTime {
publicinterfaceOnTimeRetrievedListener {
voidonTimeRetrieved(Long timestamp);
}
privatefinal DatabaseReference db;
publicServerTime(DatabaseReference db) {
this.db = db.child("serverTime");
}
/**
* Gets the server's timestamp in milliseconds.
* @param listener {@link OnTimeRetrievedListener}
*/publicvoidgetTime(final OnTimeRetrievedListener listener) {
if (listener == null) {
return;
}
db.setValue(ServerValue.TIMESTAMP).addOnCompleteListener(newOnCompleteListener<Void>() {
@OverridepublicvoidonComplete(@NonNull Task<Void> task) {
db.addListenerForSingleValueEvent(newValueEventListener() {
@OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
listener.onTimeRetrieved(dataSnapshot.getValue(Long.class));
}
@OverridepublicvoidonCancelled(DatabaseError databaseError) { }
});
}
});
}
}
Solution 3:
If you don't want to write to database to figure out the timestamp, you can get instant approximation of it. Works even offline and it's good enough for most cases.
Read value
/.info/serverTimeOffset
And add it to new Date().time()
Solution 4:
First, send an object to the firebase server
FirebaseDatabase.getInstance().getReference("serverTimeTest").setValue(new KKModel());
Model class
classKKModel{
publicString someField = "value";
publicObject creationDate = ServerValue.TIMESTAMP;
publicStringcreationDate() {
returnSimpleDateFormat.getDateInstance(DateFormat.SHORT, Locale.US).format(creationDate);
}
}
Usage
object.creationDate()
working fine test results "9/6/18"
Post a Comment for "Retrieve Servervalue.timestamp From Firebase In Android App, When Data Is Sent"