Reputation: 139
I have completed the Firebase Android Codelab here: https://codelabs.developers.google.com/codelabs/firebase-android/
When a message is sent, the name, photoUrl and text of the message have been grouped together like this:
I'm trying add a timestamp (ServerValue.TIMESTAMP) of that message in the group.
Code that's supposed to send message along with the name, photoUrl and text of the message + timestamp (originally from MainActivity.java):
mSendButton = (Button) findViewById(R.id.sendButton);
mSendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), mUsername,
mPhotoUrl, ServerValue.TIMESTAMP); //It's not supposed to be like this! What should I write instead?
mFirebaseDatabaseReference.child(MESSAGES_CHILD).push().setValue(friendlyMessage);
mMessageEditText.setText("");
mFirebaseAnalytics.logEvent(MESSAGE_SENT_EVENT, null);
}
});
With the code above, I can't add a timestamp as I want to. Why? What should I do exactly?
public class FriendlyMessage {
private String id;
private String text;
private String name;
private String photoUrl;
private Long creationDate;
public FriendlyMessage() {
}
public FriendlyMessage(String text, String name, String photoUrl, Long creationDate) {
this.text = text;
this.name = name;
this.photoUrl = photoUrl;
this.creationDate = creationDate;
}
public java.util.Map<String, String> getCreationDate() {
return ServerValue.TIMESTAMP;
}
@Exclude
public Long getCreationDateLong() {
return creationDate;
}
public void setCreationDate(Long creationDate) {
this.creationDate = creationDate;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhotoUrl() {
return photoUrl;
}
public void setPhotoUrl(String photoUrl) {
this.photoUrl = photoUrl;
}
}
See entire Firebase Android Codelab original project here (without timestamp).
Upvotes: 0
Views: 1826
Reputation: 71
do like this
public class FriendlyMessage {
private String id;
private String text;
private String name;
private String photoUrl;
private Long creationDate;
public FriendlyMessage() {
}
public FriendlyMessage(String text, String name, String photoUrl) {
this.text = text;
this.name = name;
this.photoUrl = photoUrl;
}
public java.util.Map<String, String> getCreationDate() {
return ServerValue.TIMESTAMP;
}
@Exclude
public Long getCreationDateLong() {
return creationDate;
}
public void setCreationDate(Long creationDate) {
this.creationDate = creationDate;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhotoUrl() {
return photoUrl;
}
public void setPhotoUrl(String photoUrl) {
this.photoUrl = photoUrl;
}
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("id", id);
result.put("text", text);
result.put("name", name);
result.put("photoUrl", photoUrl);
return result;
}
}
somewhere in your codes do this
Date someDate = new Date();
FriendlyMessage friendlyMessage = new FriendlyMessage("bla","bla","bla");
String key = mFirebaseDatabaseReference.child(MESSAGES_CHILD).push().getKey();
Map<String, Object> postValues = friendlyMessage.toMap();
postValues.put("creationDate", ServerValue.TIMESTAMP);
childUpdates.put("/"+MESSAGES_CHILD+"/" + key, postValues);
Map<String, Object> childUpdates = new HashMap<>();
mFirebaseDatabaseReference.updateChildren(childUpdates);
Upvotes: 2
Reputation: 11326
You must change the datatype of your creationDate variable from Long to Map. Because ServerValue.TIMESTAMP
returns a Map value.
Here's how it should look:
public class FriendlyMessage {
private String id;
private String text;
private String name;
private String photoUrl;
private Map creationDate;
public FriendlyMessage() {
}
public FriendlyMessage(String text, String name, String photoUrl, Map CreationDate) {
this.text = text;
this.name = name;
this.photoUrl = photoUrl;
this.creationDate = creationDate;
}
public java.util.Map<String, String> getCreationDate() {
return ServerValue.TIMESTAMP;
}
public void setCreationDate(Map creationDate) {
this.creationDate = creationDate;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhotoUrl() {
return photoUrl;
}
public void setPhotoUrl(String photoUrl) {
this.photoUrl = photoUrl;
}
}
Upvotes: 1