-1

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:

enter image description here

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);
        }
    });

FriendlyMessage.java:

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); 
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Me123
  • 129
  • 1
  • 2
  • 13
  • 1
    It sounds like you're looking for this: http://stackoverflow.com/questions/36658833/firebase-servervalue-timestamp-in-java-data-models-objects or this: http://stackoverflow.com/questions/33096128/when-making-a-pojo-in-firebase-can-you-use-servervalue-timestamp or this: http://stackoverflow.com/questions/37864974/how-to-use-the-firebase-server-timestamp-to-generate-date-created/37868163#37868163 (my search to find these: http://stackoverflow.com/search?q=%5Bandroid%5D%5Bandroid%5D+ServerValue.TIMESTAMP) – Frank van Puffelen Feb 02 '17 at 14:26
  • @FrankvanPuffelen Thanks for your reply! I've tried the code [from your answer](http://stackoverflow.com/a/37868163/6853372). Unfortunately, this deletes all messages from the database and I receive the timestamp multiple times. Can you please post a complete answer? I'd like to add a timestamp on messages as described in my question. I've been struggling with this issue for over 20 days as you can see from my previous Firebase question. It's so complicated! :) – Me123 Feb 02 '17 at 22:13
  • @FrankvanPuffelen Thanks for your time. I appreciate it. – Me123 Feb 02 '17 at 22:13
  • @FrankvanPuffelen I have edited my question and added my modification of FriendlyMessage.java - What changes should I make to MainActivity.java to achieve what I want? – Me123 Feb 03 '17 at 15:48
  • @FrankvanPuffelen Because the following give me an error: `FriendlyMessage friendlyMessage = new FriendlyMessage(mMessageEditText.getText().toString(), mUsername, mPhotoUrl, ServerValue.TIMESTAMP);` – Me123 Feb 03 '17 at 16:32

1 Answers1

2

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);
    }
});
Aman Grover
  • 1,621
  • 1
  • 21
  • 41
  • I already did that, it's more complicated that you think - Feel free to check out the entire project [here](https://github.com/firebase/friendlychat/tree/master/android) and test your suggestion. You'll see that you'll get all kinds of errors that I can't post here because they're too many & long. Thanks for your answer though. – Me123 Feb 02 '17 at 12:24
  • Really? Please post your complete working code then. – Me123 Feb 02 '17 at 12:26
  • you need to delete the old database from the firebase , and then run your code again. Even if I give you my code, I am doing the exact same thing I have written above. – Aman Grover Feb 03 '17 at 05:50