Write your First mesibo Enabled Application - Android

Estimated reading time: 8 minutes

In this part, we will create a simple real-time app for Android.

Prerequisites

Please go through the following prerequisites before you read further.

  • Read the section First App to learn about creating an app in mesibo console and creating users & access tokens for your app.
  • Knowledge of using Android Studio and making basic Android Apps
  • Download the source code for the First Mesibo Android App from GitHub.

First Android App

Now let’s quickly start coding:

  1. Start Android Studio.
  2. Create a new project with minimum SDK version 16 or higher.
  3. Android Studio starts Gradle and builds your project. This may take a few seconds. For more information about creating a project in Android Studio, see the Android Studio documentation. https://developer.android.com/training/basics/firstapp/creating-project.html
  4. Add mesibo SDK to your project by adding gradle dependency and performing gradle sync as explained in installation instructions. Import mesibo API.
  5. Create two users, each with the User Address 123 and 456 and App ID com.mesibo.firstapp. You can refer to the section First App to learn how to create users.
  6. Modify the token values in the source code to the ones you have generated for each of your demo users.

Example, in Java

DemoUser mUser1 = new DemoUser("xyz", "User-1", "123");
DemoUser mUser2 = new DemoUser("pqr", "User-2", "456");

Example, in Kotlin

internal var mUser1 = DemoUser("xyz", "User-1", "123")
internal var mUser2 = DemoUser("pqr", "User-2", "456")
  1. Initialize mesibo for each user, when they log in.

Initializing Mesibo

Before you send and receive real-time messages and calls, you need to initialize Mesibo. The initialization involves the following steps:

  1. Set your user credentials for authentication: access token using setAccessToken
  2. Set up Mesibo to invoke your listener class using addListener
  3. Set up database to store sent and received messages using setDatabase (Optional)
  4. Start Mesibo

You can download the source code for the First Mesibo Android App from GitHub.

import com.mesibo.api.Mesibo;

Sample mesibo initialization code, using Java in MainActivity.java

Mesibo api = Mesibo.getInstance();
api.init(getApplicationContext());

Mesibo.addListener(this);
Mesibo.setSecureConnection(true);
Mesibo.setAccessToken(user.token);
Mesibo.setDatabase("mydb", 0);

Mesibo.start();

//... Refer to the Sample Code on GitHub ...//

Sample mesibo initialization code, in MainActivity.kt

val api: Mesibo = Mesibo.getInstance()
api.init(applicationContext)

Mesibo.addListener(this)
Mesibo.setSecureConnection(true)
Mesibo.setAccessToken(user.token)
Mesibo.setDatabase("mydb", 0)

Mesibo.start()

//... Refer to the Sample Code on GitHub...//

Extend your activity to Implement mesibo.MessageListener and mesibo.ConnectionListner class and implement listeners. The easiest way is to click on the Code menu and click on Implement Methods and it will automatically generate the code for all the methods.

Sample mesibo Listeners in Java,

public class MainActivity extends AppCompatActivity implements mesibo.MessageListener, 
       Mesibo.ConnectionListener  {
	
    @Override
    public void Mesibo_onConnectionStatus(int status) {
	    // You will receive the connection status here
	    Log.d(TAG, "on Mesibo Connection: " + status);
    }

    @Override
    public boolean Mesibo_onMessage(Mesibo.MessageParams params, byte[] data) {  
	    // You will receive messages here
	   return true;
    }

    @Override
    public void Mesibo_onMessageStatus(Mesibo.MessageParams params) {
	    // You will receive status of sent messages here

    }
}

Sample mesibo Listeners in Kotlin,

 override fun Mesibo_onConnectionStatus(status: Int) {
	 // You will receive the connection status here
 }

 override fun Mesibo_onMessage(messageParams: Mesibo.MessageParams?, data: ByteArray?): Boolean {
	 // You will receive the connection status here
 }
 
 override fun Mesibo_onMessageStatus(messageParams: Mesibo.MessageParams) {
	 // You will receive the status of sent messages here
 } 
 

That’s it - you are now ready to receive your first real-time message.

Testing your first application

  1. Build and Install the Application.
  2. On the demo app, click on the Login as User1 button.
  3. Mesibo_onConnectionStatus should cycle through various status information. Finally, you should receive status=1 which indicates that your app is successfully connected to the mesibo real-time server and ready to send and receive real-time messages.
  4. We will use mesibo console to send a test message. In a later section, we will learn how to send messages from the app itself.
    • Go to Console ->Application->Users. You should see the user you have created.
    • Go to user details by clicking the Edit button. Scroll down, you will see a section to Message User
    • Enter 1000 (or anything) in the From field, check Create This User checkbox, type message and click on Send.
  5. You will instantly receive this message in your mobile app and the message will be displayed in a toast. If you check the logs, you can see that you got the message in Mesibo_onMessage listener.

Sending Messages

In the previous section, we have used mesibo console to send a message. Now we will quickly learn how to send messages from the app itself. To send messages, we will use sendMessage real-time API for which we will need destination user, message-id, and the message itself.

Invoke the following function anywhere from your code to send a text message. In a later section, we will learn how to send rich messages.

In Java,

public void onSendMessage(View view) {
    Mesibo.MessageParams p = new Mesibo.MessageParams();
    p.peer = mRemoteUser.address;
    p.flag = Mesibo.FLAG_READRECEIPT | Mesibo.FLAG_DELIVERYRECEIPT;

    Mesibo.sendMessage(p, Mesibo.random(), mMessage.getText().toString().trim());
    mMessage.setText("");
}

In Kotlin,

fun onSendMessage(view: View?) {
    val p: Mesibo.MessageParams = Mesibo.MessageParams()
    p.peer = mRemoteUser!!.address
    p.flag = Mesibo.FLAG_READRECEIPT or Mesibo.FLAG_DELIVERYRECEIPT
    Mesibo.sendMessage(p, Mesibo.random(), mMessage!!.text.toString().trim { it <= ' ' })
    mMessage!!.setText("")
}

That’s it, Two users can now send messages to each other using your app! Click on LAUNCH MESSAGE UI on the app, to try all the messaging features!

Download the First Android App Source Code from GitHub

You can download the source code from GitHub. Once you have set up two users and modified the tokens in the source code, you can install the app on two devices and try it out.

On one device, you can log in as User-1. On another device, you can log in as User-2. These two users can now text each other, send files to each other, make audio and video calls, etc. We will learn more about sending files and making calls in the upcoming sections.

First Mesibo Application - iOS >>

mesibo, android, ios