0

If I will take date from the system (local date in UTC) then, the problem occurs when the user changes the date, if I will use server date time then I can not able to send a message when I am offline, this application is developed in android using firebase real-time DB

Tanveer Munir
  • 1,956
  • 1
  • 12
  • 27
Jignesh
  • 55
  • 2
  • 12

1 Answers1

0

ServerValue.TIMESTAMP

public static final Map<String, String> TIMESTAMP

Also: Google Play services

A placeholder value for auto-populating the current timestamp (time since the Unix epoch, in milliseconds) by the Firebase Database servers.

Example

Ref : https://stackoverflow.com/a/37868163/9909365

ref.addValueEventListener(new ValueEventListener() {
    public void onDataChange(DataSnapshot dataSnapshot) {
        System.out.println(dataSnapshot.getValue()); 
    }

    public void onCancelled(DatabaseError databaseError) { }
});
ref.setValue(ServerValue.TIMESTAMP);

Extra

Handling Latency

Server Timestamps

The Firebase Realtime Database servers provide a mechanism to insert timestamps generated on the server as data. This feature, combined with onDisconnect, provides an easy way to reliably make note of the time at which a Realtime Database client disconnected:

//Java
DatabaseReference userLastOnlineRef = FirebaseDatabase.getInstance().getReference("users/joe/lastOnline");
userLastOnlineRef.onDisconnect().setValue(ServerValue.TIMESTAMP);


//Kotlin
val userLastOnlineRef = FirebaseDatabase.getInstance().getReference("users/joe/lastOnline")
userLastOnlineRef.onDisconnect().setValue(ServerValue.TIMESTAMP)
Ashvin solanki
  • 4,802
  • 3
  • 25
  • 65