Before you begin, you need a few things to set up in your environment:
Android Studio 3.0.0 or later
Android SDK Build-Tools 26.0.2 or later
A Firebase project correctly configured and the Chat21 Firebase cloud functions installed. Detailed instructions here
Create a Firebase project
Sign up on Firebase and create a project. Please refer directly to Firebase https://firebase.google.com to accomplish and better understand this task. Chat21 relies on Firebase as the backend, so it's really important for you to acquire familiarity with Firebase and all of his services.
Setup the backend for your project
After you successfully created a Firebase project you must setup the backend. Please follow this link to install Chat21 cloud functions on your just created Firebase project.
Now with the setup of your Firebase project and backend functions installed you can enable email authentication to provide an easy way for your app to sign in with email and password.
Enable email signin in Firebase console:
Create a user to test chat functions:
Choose email and password:
Add the user with “ADD USER” button.
Create Android Studio project
This tutorial will focus on the creation of a simple single view application. In the next tutorial you will approach the creation of a more realistic multi tab application (similar to Whatsapp).
First open Android Studio, select Start a new Android Studio Project and insert the project info using MyChat as project name and insert your team.
NOTE: Take note of the Package name, it will be used in the following steps
Select the Phone and Tablet > API Android 19: 4.4 (Kitkat) as minimum SDK
Select Empty Activity
Insert the Activity and the Layout name
Create the Firebase Android App
Switch on the project on Firebase, go to the Firebase Console > Project Overview and add a Android App to your project by clicking on “Add Android App” and follow the setup steps.
When prompted, enter your app’s Package name (you have pinned previously). It’s important to enter the Package name your app is using, this can only be set when you add an app to your Firebase project.
At the end, you’ll download a google-services.json file. You can download this file again at any time.
Now add this file to your Android project App root
Add Firebase libs to the project
Now go back to your Android project and add firebase libraries to your project.
First, add rules to your root-levelbuild.gradle file, to include the google-services plugin and the Google’s Maven repository:
Then, in your module Gradle file (usually the app/build.gradle), add the apply plugin line at the bottom of the file to enable the Gradle plugin:
Add the following code to the onCreate method of your Main Activity and substitute variable in bracket using the previously created user’s email and password. For APP_ID use "chat" (this is the tenant's name and MUST be the same for all of your Clients, ex. iOS)
If everything is correct you will see the conversations’ history with no conversations.
As you can see, in the authWithEmail completion block we use the createContactFor method to create a contact on the remote backend for the currently signed user. In this way every user will add his metadata to contacts as soon as he sign in. The button on the bottom right corner open the contacts list.
You will see yourself listed. If you want you can chat with yourself but it’s better to create another user and sign in on a chat installed on another device (or simulator instance).