Home > Developers > Documentation > Android

MQTTCHAT Android

MQTTCHAT Android is an android library that provides full chat functionality and can be integrated into any android application from API 14.

1- Add MQTTCHAT Artifactory repository to the list of Maven repositories in your top level build.gradle file:

 
allprojects {
    repositories {       
        maven {
            url "http://mqttchat.telifoun.com:8081/artifactory/libs-release-local"
            credentials {
                username = "mqttchat"
                password = "telifoun"
            }
        }
    }
}   
                    

2- You can then simply add MQTT CHAT artifacts as a dependencies in the build.gradle file of your main project:


dependencies {
    implementation 'com.telifoun.mqttchat:mqttchat-core:1.0.0'
    implementation 'com.telifoun.mqttchat:mqttchat-gui:1.0.0'
}  

In Application class of your Android App add the following code to init MQTTCHAT: You can get your own App_Id and App_Secret after registration to MQTTCHAT.

        
     new Mqttchat.getBuilder()
             .context(this.getApplicationContext())
             .appName("Application Name")
             .appIcon(R.drawable.ic_launcher)
             .domain("your_domain.com")
             .appId(App_Id)
             .appSecret(App_Secret)
             .showBackButton(true)
             .debugMode(true)             
             .build();
                

Below all possible parameters for initializing MQTTCHAT.

ParameterDescriptionNote
*contextApplication context Mandatory
*domainApplication domain Mandatory
*appIdMQTTCHAT APP_IDMandatory
*appSecretMQTTCHAT APP_SecretMandatory
useFriendsEnable or not useFriends optionOptional
mqttServerMqtt Server IPOptional
mqttPortMqtt Port Optional
idleTimeoutIdle timeout to go to away statusOptional
AppNameApplication NameOptional
AppIconApplication IconOptional
debugModeEnable or not debug mode to show trace in logcatOptional
showBackButtonShow or not back button in main GUI.Optional

1 - In LoginActivity After user login success, You can connect user to MQTTCHAT using login function.

  
  Mqttchat.getmInstance().logIn(getApplication(), userId, new Callback() {
   @Override
   public void OK(Object o) {
   }
   @Override
   public void KO(String s) {
   }
   });

2 -Once the user is connected to MQTTCHAT you can launch chat interface whenever you want using this code :

  
  Mqttchat.getmInstance().lounchMqttChat(getApplicationContext(), new Callback() {
   @Override
   public void OK(Object o) {
   }
   @Override
   public void KO(String s) {   
   }
});

3 - To log out a user ::

  
   Mqttchat.getmInstance().logOut(new Callback() {
  @Override
   public void OK(Object o) {
   }
   @Override
   public void KO(String s) {   
   }
});

You can capture all MQTT CHAT events using a mqttchatListener :

  
Mqttchat.getmInstance().addMqttchatListener(new mqttchatListener() {
            @Override
            public void onLoadComplete() {
              Log.i(Tag,"MQTT Chat load complete");
            }

            @Override
            public void onUserPresenceUpdate(int i, String s, boolean b, boolean b1, String s1) {
             Log.i(Tag,"User presence update : userId:"+i +"; new status:"+s+" ;voice capacity:"+b +" ;video capacity:"+b1+"; timestamp:"+s1);
            }

            @Override
            public void onSendMessage(int i, Message message) {
                Log.i(Tag,"Logged user send message to userId :"+i +"; message:"+message.toString());
            }

            @Override
            public void onAckMessage(int i, Ack ack, Message message) {
                Log.i(Tag,"Logged user received Ack from server for message sent to userId :"+i +"; ack :"+ack.toString()+"; message:"+message.toString());
            }

            @Override
            public void onRejectMessage(int i, String s, Message message) {
                Log.i(Tag,"Logged user has sent message to userId:"+i+"; message was rejected by server because of :"+s+"; and message content :"+message.toString());
            }

            @Override
            public void onIncomingMessage(int i, Message message) {
                Log.i(Tag,"Logged user received new message from userId:"+i+"; message content:"+message.toString());
            }

            @Override
            public void onUserTyping(int i, boolean b) {
                Log.i(Tag,"A user  userId:"+i+" is currently typing to logged user; is typing :"+b);
            }

            @Override
            public void onReadingMessage(int i, int i1, String s) {
                Log.i(Tag,"A user userId:"+i+"; is reading message id:"+i1+" ; reading date :"+s);
            }

            @Override
            public void onNotReadMessagesCountUpdate(int i) {
                Log.i(Tag,"Number of unread messages is changed for logged user, new number is :"+i);
            }

            @Override
            public void onError(int i, String s) {
                Log.e(Tag,"MQTTCHAT error code :"+i+";message :"+s);
            }
        });

MQTTCHAT android application contains 6 plugins by default: smileys, stickers, pictures from DISK, pictures from CAM, records and Visio Notifications.

Simleys

Is used to send smileys.

Stickers

Is used to send some Stickers, you can catch sticker sending event using stickersListener.

   
Stickers plugin=(Stickers) Mqttchat.getmInstance().getPluginByName(pluginA.stickers);
        plugin.setListener(new stickersListener() {
            @Override
            public void onMqttchatSendSticker(int toUserId,String stickerCode) {
               // your custom code here 
            }
        });

Send Picture from DISK

Is used to send pictures from local Disk, you can catch picture sending event using pictureDiskListener.

   
pictureDisk plugin=(pictureDisk) Mqttchat.getmInstance().getPluginByName(pluginA.pictures_disk);
        plugin.setPictureDiskListener(new pictureDiskListener() {
            @Override
            public void onMqttchatSendPictureDisk(int toUserId,String urlMin,String urlMax) {
              //your custom code here  
            }
        });

You can also adjust some plugin parameters:

ParameterDescriptionDefault value
MAX_PHOTOS_TO_DROPmaximum number of photos to be sent simultaneously6
MAX_PHOTO_SIZESingle photo max size1 Mo
 
plugin.setMAX_PHOTOS_TO_DROP(3);
plugin.setMAX_PHOTO_SIZE(2);

Send Picture from Camera

Is used to send pictures from Camera, you can catch picture sending event using pictureCamListener.

   
pictureCam plugin=(pictureCam) Mqttchat.getmInstance().getPluginByName(pluginA.pictures_cam);
        plugin.setPictureCamListener(new pictureCamListener() {
            @Override
            public void onMqttchatSendPictureCam(int toUserId,String urlMin,String url) {
              //your custom code here  
            }
        });

You can also adjust some plugin parameters:

ParameterDescriptionDefault value
MAX_PHOTOS_TO_DROPmaximum number of photos to be sent simultaneously3
 
plugin.setMAX_PHOTOS_TO_DROP(1);

Send Voice Notes

Is used to send voice notes, you can catch voice notes sending event using recordsListener.

   
records plugin=(records) Mqttchat.getmInstance().getPluginByName(pluginA.records);
        plugin.setRecordsListener(new recordsListener() {
            @Override
            public void onMqttchatSendRecord(int toUserid,String duration,String Url){
             //your custom code here   
            }
        });

You can also adjust some plugin parameters:

ParameterDescriptionDefault value
RECORD_TIME_LIMITmaximum recording time of one single record1 min
 
plugin.setRECORD_TIME_LIMIT(1);

Visio Notifications

Is used for missed and completed calls notifications.

First add module class that extends moduleA abstract class to your App.


public class userProfile extends com.telifoun.mqttchat.modules.moduleA {
    /**
     *
     * @param ctx
     * @param Name
     * @param Label
     */
    public userProfile(android.content.Context ctx, String Name, String Label) {
        super(ctx, Name, Label);
    }    
    @Override
    public void moduleMenuItemCLicked() {
        /* lounch profile activity on menu item click */
        Intent i = new Intent(getMessageActivity(), ProfileActivity.class);
        getMessageActivity().startActivity(i);
    }
}
    

Then Add module to MQTTCHAT in Application class.


userProfile mProfile=new userProfile(getApplicationContext(),"User Profile","Go to Profile");
Mqttchat.getmInstance().getModules().add(mProfile);

See MQTTCHAT github demo Application : mqttchat-android-demo

Try it on google play store :

2020 © Telifoun.MQTTCHAT Inc.