Introduction

Are you tired of the same old, boring chat apps? Have you ever wished to build your own app with exclusive messaging features and cutting-edge capabilities?

Great! You are at the right place.

In this tutorial, you’ll learn how to enable chat features in your Android app using Java. We will use MirrorFly SDK to perform this.

Now, it’s time to get started!

MirrorFly - Messaging Features Overview

When you integrate MirrorFly SDK into your chat app, you will be able to add a wide range of messaging features within a short time. Here is a list of the top features you'll get from MirrorFly’s package.

  • Topic-based chat
  • Send / Receive Direct Messages
  • Attach Files / Images or Media
  • Message Status
  • Group Chat
  • Unread Messages Count
  • Chat tags
  • Push notifications

Explore more features from MirrorFly here

Steps To Build An Android App In Java

Download The Java SDK From MirrorFly Account

Step 1 : The first step of this chat app development is to create an account with MirrorFly.

Step 2 : In our official contact page, fill in your details. Our expert will get in touch with you for the next steps with the account.

Step 3 : Log into your account and get the license key.

#

Prep The App

There are 2 ways to create your online chat application in Java. You can integrate MirrorFly API or SDK into our sample app / your existing app or make your own chat app from scratch using Android Studio IDE.

Basic Requirements

  • Android Lollipop 5.0 (API Level 21) or above
  • Java 7 or higher
  • Gradle 4.1.0 or higher

Basic Requirements

  • Android Lollipop 5.0 (API Level 21) or above
  • Java 7 or higher
  • Gradle 4.1.0 or higher

Sample App

Download our Sample App here

Initialize The Android Chat SDK

Before you begin using the SDK, make sure you meet some of the basic requirements. Inside the onCreate() method of your Application class, use the following method from ChatManager to provide the required data.


                     ChatManager.initializeSDK("LICENSE_KEY", (isSuccess, throwable, data) -> {  
                           if(isSuccess){  
                               Log.d("TAG", "initializeSDK success ");  
                           }else{  
                               Log.d("TAG", "initializeSDK failed with reason "+data.get("message"));  
                           }  
                     });  
                        

Add MyApplication

Include the created MyApplication in the AndroidManifest.xml file.


                <?xml version="1.0" encoding="utf-8"?>  
                <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
                    <package="com.example.uikitapplication">  
                    <application  
                                 android:name=".MyApplication"  // Add this line.  
                                 android:icon="@mipmap/ic_launcher"  
                                 android:label="@string/app_name"  
                                 android:roundIcon="@mipmap/ic_launcher_round"  
                                 android:theme="@style/AppTheme">  
                                <activity android:name=".MainActivity">  
                                <intent-filter>  
                                <action android:name="android.intent.action.MAIN" />  
                                <span> ...  
                                <span>   
                                <span>   
                                </activity>  
                                </application>  

                

User Registration

Use the following method to register a user in sandbox Live mode based on the provided setIsTrialLicenceKey.


                     FlyCore.registerUser(USER_IDENTIFIER, (isSuccess, throwable, data ) -> {  
                         if(isSuccess) {  
                             Boolean isNewUser = (Boolean) data.get("is_new_user");  
                             String userJid = (String) data.get("userJid");  
                             JSONObject responseObject = (JSONObject) data.get("data");  
                             String username = responseObject.getString("username");  
                             String password = responseObject.getString("password");  
                         } else {  
                         // Register user failed print throwable to find the exception details.  
                         }  
                          });  
                        
                

Connect To The Chat Server

After a successful registration, ChatSDK will automatically try to connect to the Chat Server. It also monitors changes in the application lifecycle, managing connections and disconnections to the Chat Server accordingly.

Observe Connection Events

After you set the ChatConnectionListener, you can get updates on the connection status in the callback method as shown below.


                      ChatManager.setConnectionListener(new ChatConnectionListener() { 
                     @Override 
                     public void onConnected() { 
                         // Write your success logic here to navigate Profile Page or 
                         // To Start your one-one chat with your friends 
                     } 
                 
                     @Override 
                     public void onDisconnected() { 
                         // Connection disconnected 
                     } 
                 
                     @Override 
                     public void onConnectionFailed(@NonNull FlyException e) { 
                         // Connection Not authorized or Unable to establish connection with server 
                     } 
                 
                     @Override 
                     public void onReconnecting() { 
                         // Automatic reconnection enabled 
                     } 
                 }); 
                

Send A One-to-One Message

To send a message to another user, utilize the method provided below.


                     TextMessage textMessage = new TextMessage();  
                     textMessage.setToId(TO_JID);  
                     textMessage.setMessageText(TEXT);  
                     FlyMessenger.sendTextMessage(textMessage, (isSuccess, error, chatMessage) -> {  
                         if (isSuccess) {  
                             // you will get the message sent success response  
                         }  
                     });  
                    

Receive a One-to-One Message

To receive and observe all incoming messages sent to you, you must initialize the observer MessageEventsListener.


                     ChatEventsManager.setupMessageEventListener(new MessageEventsListener() {  
                         @Override  
                         public void onMessageReceived(@NotNull ChatMessage message) {  
                         //called when the new message is received  
                         }  
                     });  
                        

Furthermore, these listeners will be triggered only when a new message is received from another user. For more details, please visit the specified callback listeners.


                     @Override  
                     public void onMessageReceived(@NonNull ChatMessage message) {  
                         super.onMessageReceived(message);  
                         // received message object  
                     }  
                
mirrorfly

Looking To Build An Android Chat App?

Drive 1 billion + conversations on any Android, iOS, or Web app with 500+ chat features & 100+ UI components.

  • Topic-based Chat
  • Upload Large Files
  • Multi-tenancy support
  • Lite Chat App
  • Hire Dedicated Team
#
Integrate Our SDKs In Just 10 Mins!

Integrate Our Video, Voice & Chat SDKs into any app in < 30 mins

Contact Sales
#
Get Started With Our Self-hosted Chat Solution Today!

Get Full Access To Our Customizable Video, Voice & Chat SDKs!

Request Demo
Need to build a complete chat app? Let our experts help you out.
Hire Our Team