What's going on with com.firebase.client.ServerValue.TIMESTAMP?
I am trying to build a chat app using Firebase on the web and build a client and Android. In my web app, I am sending the Firebase server timestamp along with the message, but I seem to be having Android issues. Using the Firebase Opensource Android chat app, I used the default Chat.java class, but when I try to send a timestamp using ServerValue.TIMESTAMP
, I get an error because I assume it will return int
, but it will Map
.
I am currently using a workaround and getting the time from the device itself, but I try to keep it consistent and reduce room for error if people in time zones use the app.
Here's my current work around the 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;
}
}
source to share
Too long to go into comments. I was referring to something similar that I have used several times.
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();
});
However, in javascript, the principle should be the same in Java.
source to share