Dr. J
Dr. J

Reputation: 51

What does com.firebase.client.ServerValue.TIMESTAMP respong with?

I'm trying to build off a chat application using Firebase on the web, and create and Android client. In my web app, I send the Firebase Server's time stamp along with the message, but I seem to be having a bit of trouble on Android. Using Firebase's Opensource chat app for Android, I have used the default Chat.java class, but when I try to send the time stamp using ServerValue.TIMESTAMP I get an error because I assume it would return an int but it returns a Map.

Right now I'm using a workaround and getting the time from the device itself, but I'm trying to keep it consistent and reduce room for error if people across time zones are going to be using the app.

Here's my current work around block

private void sendMessage() {
    EditText inputText = (EditText)findViewById(R.id.messageInput);
    String input = inputText.getText().toString();
    Long timestamp = System.currentTimeMillis()/1000;
    if (!input.equals("")) {
        // Create our 'model', a Chat object
        Chat chat = new Chat(name, input, timestamp, userID);
        // Create a new, auto-generated child of that chat location, and save our chat data there
        ref.push().setValue(chat);
        inputText.setText("");
    }
}

And Chat.java

public class Chat {

private String from;
private String text;
private int userID;
private Long timestamp;

// Required default constructor for Firebase object mapping
@SuppressWarnings("unused")
private Chat() { }

Chat(String from, String text, Long timestamp, int userID) {
    this.from = from;
    this.text = text;
    this.timestamp = timestamp;
    this.userID = userID;
}

public String getFrom() {
    return from;
}

public String getText() {
    return text;
}

public Long getTimestamp() {
    return timestamp;
}

public int getuserID() {
    return userID;
}

}

Upvotes: 1

Views: 3202

Answers (1)

webduvet
webduvet

Reputation: 4292

It is too long to go into comment. I meant more something like this, which I used on number of occasions.

var chatUserRef = new Firebase("your url to chat user");
chatUserRef.child('time')
  .set(Firebase.ServerValue.TIMESTAMP)
  .on('value', function(snapshot){
     var current_server_time = snapshot.val();
  });

It is in javascript nevertheless the principle should be the same in Java.

Upvotes: 2

Related Questions