How to save the current date/time when I add new value to Firebase Realtime Database

前端 未结 3 1017
猫巷女王i
猫巷女王i 2020-11-22 05:55

I want to save the current date/time in specific field when I add new value to Firebase Realtime Database via control panel.

How can I achieve that?

Please

3条回答
  •  栀梦
    栀梦 (楼主)
    2020-11-22 06:37

    Alex Mamo is right but then this is how you should pass incase you call it from a model class

    public class FirebMessage {
        public String message;
        public String senderPhoneNumber;
        public String receiverPhoneNumber;
        public Map time;
    
        public FirebMessage() {
        }
    
        public FirebMessage(String message, String senderPhoneNumber, String receiverPhoneNumber, Map time) {
            this.message = message;
            this.senderPhoneNumber = senderPhoneNumber;
            this.receiverPhoneNumber = receiverPhoneNumber;
            this.time = time;
        }
    

    Then consume as follows

     private void writeNewMessage(String message, String receiver,String sender) {
    
            FirebMessage firebMessage = new FirebMessage(message, receiver,sender,ServerValue.TIMESTAMP);
            mDatabase.child("messages").push().setValue(firebMessage, new DatabaseReference.CompletionListener() {
                @Override
                public void onComplete(DatabaseError databaseError, DatabaseReference databaseReference) {
                    if (databaseError != null) {
                       Log.e(TAG,"Data Not saved");
    
                    } else {
                        Log.e(TAG,"Data saved successfully");
                    }
                }
            });
        }
    

提交回复
热议问题