Adding Firebase Functionality to Your Android Application3:45 with Michael Lustig
Create a `DatabaseReference` object that points to our messages node.
Now let's try some Android code to post a message to Firebase. 0:00 Before we do anything in the messaging activity, 0:03 I need to uncomment the Firebase annotation under messages class. 0:06 In order to send a message to Firebase, we need a few things. 0:12 First, we need a name for the Firebase reference we wanna push to. 0:17 For our example, 0:21 we're going to push to a node of our Firebase that we'll label messages. 0:22 This label can be anything, but since we're going to store messages, 0:25 I felt like this would be an appropriate name. 0:28 The Firebase SDK uses what's called the database reference to 0:31 talk with these nodes I mentioned above. 0:34 So we're going to create a database reference object that points to our 0:36 messages node. 0:39 From there, we'll be able to push a message to Firebase. 0:40 In order to create the database reference we want, we ask the Firebase database for 0:44 its singleton instance and then request the Firebase databases reference to 0:48 the messages node by asking the Firebase for the messages database reference. 0:52 We do this by calling the getReference method on the Firebase object, and 1:26 passing in our reference's title, MESSAGES. 1:30 Now that we have an object that has a reference to our MESSAGES node, 1:33 we can write a method that pushes the typed message to Firebase. 1:36 We'll get the message content and the user name of the logged in user using the user 1:54 utility method that handily provides the user name. 1:57 Our message POJO also requires a time stamp, so 2:11 we'll send the current system time. 2:13 Next, we'll create our message object. 2:16 Finally, we call push on our messages reference object. 2:30 If you think about real time functionality, and giving IDs to children 2:36 of this node, you would realize that this is not an easy problem to solve. 2:39 What if two clients attempt to post an object within the same millisecond? 2:42 Would they get the same ID? 2:46 How would this be resolved? 2:47 Well, Firebase solves that issue for us by issuing 2:50 objects we put in the database IDs that are assured to be unique. 2:52 We can get a new ID by calling push on our reference messages object, and 2:56 then setting the value to the message object we'd created above. 3:00 Believe it or 3:04 not that's all the code you have to write to send a message to Firebase. 3:05 We also have to call this method when the send message method is called in our 3:08 activity but that's it. 3:12 Let's go ahead and run this and check out the behavior. 3:22 I'll pull up the Firebase database console on the side, so 3:25 we can watch what happens in real time. 3:27 Nothing happened, what's going on? 3:37 Well it turns out there's just one last thing we need to do in this 3:40 project to be able to post messages to Firebase. 3:43
You need to sign up for Treehouse in order to download course files.Sign up