Skip to content

Instantly share code, notes, and snippets.

@llSourcell
Created February 25, 2016 23:47
Show Gist options
  • Save llSourcell/a0dbe4a5dd7189269c9d to your computer and use it in GitHub Desktop.
Save llSourcell/a0dbe4a5dd7189269c9d to your computer and use it in GitHub Desktop.
#Android Chat App in 5 Min
##Intro
Hey guys, I’m Siraj and I’m a developer educator at Twilio. Today we’re going to be going through an Android chat app in 5 minutes. Let’s get started.
##App Walkthrough
Let’s see how the app works.
It uses Twilio’s IP Messaging SDK to enable the chat functionality. We’ll start off with a login screen where we can type in a username. Once we’ve typed in a username, we can tap login. The next screen will show all the channels available for us to join. These are a combination of public channels, channels that you’ve created, and private channels that you’ve been invited to. Let’s go ahead and join one of these channels. Once we’ve joined a channel, we’ll see the message window. This will list previously sent messages by every user that’s ever been in the channel along with the timestamps of those messages. Pretty cool, right? We can go ahead and type in a message and hit send for it to appear to everyone. That’s basically it. Let’s check out the code and see how we’re making this functionality happen
##Logging in/Authentication
When we first open our app in Android Studio, it seems like there is a lot of code. Don’t worry it’s all good, most of these .java files are just boilerplate dependencies that will support the unique code that we’ll want to build for this app. In Android apps, Activities are where most of the magic happens. Activities are a single, focusesd task that the user can do. So you’ll have one activity per task. In our Android Manifest file we can set app specific details, such as what the first activity our app launches is. In this case, we’ll choose Launch activity to be numero uno.
In the launch activity there isn’t much code. we’re just saying let’s call this getLaunchClass method. That’s it. We can see here that getLaunchClass is asking an object called the SessionManager if the user is logged in or not. If they are, it’ll start the mainchatactivity which just lets them start chatting. If not, it’ll go to the login activity which shows the login page. Alright, makes sense. So SessionManager is a helper class that manages an IP messaging session. You can get and set user credentials in this class. The actual session itself is created from an instance of TwilioChatApplication helper class. That class just does one thing. It initialize the IPMessagingClient client. So the session manager calls the twiliochatapplication with calls the ipmessagingclient.
So let’s assume we’ve never logged in before. In the LoginActivity, we’ll start off by initializing the client using the TwilioChatApplication singleton. The client is the hub around which all messaging activity occurs. You can use your client to create and join channels and send and receive messages in those channels. We have an event listener setup for the login button. Once a user types in a username and hits login, the SessionManager method createLoginSession is called with the username input as the parameter. This method saves the usernames and the is_logged_in boolean to the shared preferences with the commit command of the editor object.The shared preferences object called editor, just stores data as key-value pairs in memory for our app. It’s a native Android class. it’s super useful for stuff keeping login credentials in memory that are easily accessible throughout the app.
We can then initialize our ip messaging client which just calls the connectClient method of the messagingClient. OnLoginFinished as an event is fired when login is complete, then the mainchatactivity will appear to allow us to chat. Let’s check out the IPMessagingManager class. It creates the client using an access manager. The access manager gets the access token from your IPM server. The access token is what authenticates your client with IPM. It lets your app communicate with other IPM enabled apps. That’s all it takes!
##Channel Selection
Once we’ve logged in, the MainChatActivity is called. We’ll start off in the OnCreate method just drawing our view. We’ll want to list all of our available channels. In order to do that, we can initialize the ChannelManager. Then in our checkTwilioClient method, we’ll be populating the channel list using the PopulateChannels method. This method uses our channelManager to get the list of channels available. Channelmanager wraps a bunch of channel related API calls into easy to use methods. Creating, joining, and listening to channel events is all possible. Once we’ve selected a channel, the setChannel method is called which joins the channel in question inside of this completion handler.
The join channel method will set the chatFragment to the selected channel. The chatFragment is a UI component that has its own java class. Its basically an embedded view in our activity that we can change as events occur. In this case, a channel is selected, so we’ll want to change it from the channel selection screen to a chat window where you can chat with other people.
##Chatting
The MainChatFragment class is where we store all the messaging logic. We load all our messages into an in-memory message array, and use the message adapter helper class to display our messages. We can send messages with the send message method thats fired every time someone pressed send in the UI. At the bottom we see all of our message delegate methods. Each of them is fired when a different messaging related event occurs.
##Outro
That’s pretty much it! For more information, check out the links in the description below. You’ll find a link to the repository for this app as well as detailed documentation on ways of integrating
the IP messaging SDK into your app. Thanks for watching!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment