Issue
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:
How do I add a timestamp (ServerValue.TIMESTAMP) of that message in the group? Can you please provide a code sample?
Code that sends message along with the name, photoUrl and text of the message (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);
mFirebaseDatabaseReference.child(MESSAGES_CHILD).push().setValue(friendlyMessage);
mMessageEditText.setText("");
mFirebaseAnalytics.logEvent(MESSAGE_SENT_EVENT, null);
}
});
public class FriendlyMessage {
private String id;
private String text;
private String name;
private String photoUrl;
public FriendlyMessage() {
}
public FriendlyMessage(String text, String name, String photoUrl) {
this.text = text;
this.name = name;
this.photoUrl = photoUrl;
}
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 project here.
My attempt to modify FriendlyMessage.java:
public class FriendlyMessage {
private String id;
private String text;
private String name;
private String photoUrl;
private HashMap<String, Object> dateCreated;
private HashMap<String, Object> dateLastChanged;
public FriendlyMessage() {
}
public FriendlyMessage(String text, String name, String photoUrl, HashMap<String,Object> dateCreated) {
this.text = text;
this.name = name;
this.photoUrl = photoUrl;
this.dateCreated = dateCreated;
HashMap<String, Object> dateLastChangedObj = new HashMap<String, Object>();
dateLastChangedObj.put("date", ServerValue.TIMESTAMP);
this.dateLastChanged = dateLastChangedObj;
}
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 HashMap<String, Object> getDateLastChanged() {
return dateLastChanged;
}
public HashMap<String, Object> getDateCreated() {
//If there is a dateCreated object already, then return that
if (dateCreated != null) {
return dateCreated;
}
//Otherwise make a new object set to ServerValue.TIMESTAMP
HashMap<String, Object> dateCreatedObj = new HashMap<String, Object>();
dateCreatedObj.put("date", ServerValue.TIMESTAMP);
return dateCreatedObj;
}
// Use the method described in http://stackoverflow.com/questions/25500138/android-chat-crashes-on-datasnapshot-getvalue-for-timestamp/25512747#25512747
// to get the long values from the date object.
@Exclude
public long getDateLastChangedLong() {
return (long)dateLastChanged.get("date");
}
@Exclude
public long getDateCreatedLong() {
return (long)dateCreated.get("date");
}
}
Here's the code that I experimented with for timestamp (to show you that I have made efforts - feel free to use it in your answer):
DatabaseReference ref = FirebaseDatabase.getInstance().getReference();
String key = ref.push().getKey(); // this will create a new unique key
Map<String, Object> value = new HashMap<>();
value.put("timestamp", ServerValue.TIMESTAMP);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
Long timestamp = snapshot.getValue(Long.class);
System.out.println(timestamp);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
ref.child(key).setValue(value);
Solution
In the model class FriendlyMessage
add another field named:
String timeStamp;
public FriendlyMessage(String text, String name, String photoUrl, String timeStamp) {
this.text = text;
this.name = name;
this.photoUrl = photoUrl;
this.timeStamp = timeStamp;
}
and when calling the constructor pass the timeStamp in the format you like:
mSendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), mUsername,
mPhotoUrl, /*time stamp in the format you like*/ timeStamp);
mFirebaseDatabaseReference.child(MESSAGES_CHILD).push().setValue(friendlyMessage);
mMessageEditText.setText("");
mFirebaseAnalytics.logEvent(MESSAGE_SENT_EVENT, null);
}
});
Answered By - Aman Grover
Answer Checked By - Marie Seifert (JavaFixing Admin)