ANDROID SDK

In-App Chat Android SDK

Overview

eRTC SDK is secure, easy to integrate, deploy and manage.

The following principles are followed during development:

  • Modularity: Code is segregated into multiple modules and follows chain of responsibility model
  • Ease of Integration: Maven artifacts can be downloaded from a public hosted url. eRTC SDK can be integrated into any mobile app within minutes
  • Scalability: SDK is highly scalable and can support Enterprise traffic
  • Built on Latest Tech Stack
  • Data ownership: SDK provides a data layer to effectively manage and control data
  • Firebase powered by Google: Highly reliable and scalable
  • Reactive using RxAndroid

Features

  • Individual messages
  • Push notifications
  • Text, Image, Video, Audio messages
  • Typing Indicators
  • Delivered and Read receipts
  • Chat history
  • Chat user list
  • Contact details

Quickstart

  • To start with an existing app, just initialize the SDK in your Application’s onCreate() method and start using the SDK features
  • Please note that we are using Java 8 in our SDK, sso you'll need to add Java 8 support in the compile options
  • We are using AndroidX in our SDK, so please add AndroidX support in gradle.properties
  • Once you do these prerequisites, you are ready to start using our SDK

API documentation

Validate the namespace of your app

eRTCSDK.tenant().validate(namespace).subscribe(
   Consumer { this.success(it) }, error
)

Log in with username and password

login(AccountDetails details);
E.g. :-
eRTCSDK.tenant().login(AccountDetails.username(username, password)).subscribe(
Consumer { this.success(it) }, error
)

Forgot password

eRTCSDK.tenant().forgotPassword(AccountDetails.forgotPwd(namespace)).subscribe(
   Consumer { this.success(it) }, error
)

Change password

eRTCSDK.tenant().changePassword(AccountDetails.changePwd(oldPassword, newPassword)).subscribe(
   Consumer { this.success(it) }, error
)

To initiate chat, you have to create a thread with the recipient

eRTCSDK.chat().createThread("testthread", user.id)
//testhread is thread name, you can choose to not provide

Code example:

disposable.add(eRTCSDK.chat().createThread("", user.id).subscribe({
//open chat screen to begin chatting
}, {
//handle error
}))

Sending a text message

API signature : sendMessage(String text, String threadId);

String textMessage = “Hi!! What are you up to”;
eRTCSDK.chat().sendMessage(textMessage, threadID);

Sending a media message

sendMedia(String mediaPath, String threadId, String mediaType)
//mediaType = ‘image’/’audio’/’video’

eRTCSDK.chat().sendMedia(
filePath,             //filePath
threadID,
messageRecord.msgType          //msgType = ’image’/’audio’/’video’
)

Code example:

For capturing photos:
Create a file_paths.xml file in res>xml folder, and add the content :-

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files"
   path="." />
</paths>

And in AndroidManifest.xml, add this under application tag

<provider
   android:name="androidx.core.content.FileProvider"
   android:authorities="${applicationId}.provider"
   android:exported="false"
   android:grantUriPermissions="true">
   <meta-data
      android:name="android.support.FILE_PROVIDER_PATHS"
      android:resource="@xml/file_paths"></meta-data>
   </provider>

To get file for startTakePhotoActivity, use this

fun getPhotoImageFile(): File {
var cameraImage:File? = null
val mediaSelector = MediaSelector()
cameraImage = mediaSelector.getPictureFile()
return cameraImage;
}

fun capturePhoto(activity: Activity, cameraImage: File, requestCode: Int) {
Permissions.with(activity).request(Manifest.permission.CAMERA).ifNecessary()
    .withPermanentDenialDialog(activity.getString(R.string.
   attachment_take_photos_denied))
    .onAllGranted {
       val mediaSelector = MediaSelector()
       mediaSelector.startTakePhotoActivity(activity, cameraImage)
   }.execute()
}

Choose photos

fun selectGallery(activity: Activity, requestCode: Int) {

Permissions.with(activity).request(Manifest.permission.WRITE_EXTERNAL_STORAGE).ifNecessary()

.withPermanentDenialDialog(activity.getString(R.string.attachment_photos_videos_or_audio_denied))
   .onAllGranted {
      val mediaSelector = MediaSelector()
      mediaSelector.startChooseImageActivity(activity)
   }.execute()
}

Load user list

getUsersInSync(tenantId: String, lastId: String?)

E.g : Code below:

eRTCSDK.user().users
   .subscribeOn(Schedulers.io())
   .observeOn(AndroidSchedulers.mainThread())
   .subscribe(
   this::onUserListSuccess,      //handle user list success
   this::onUserListError      //handle user list failure
)

private fun onUserListSuccess(userList: List) {
}

private fun onUserListError(throwable: Throwable) {
}

How to add the eRTC SDK to your Android app

Integration with an existing project is simple. You can add the core libraries using Gradle.

Gradle

To download the SDK, you will need to include the repository manually:

  • Add repository
    repositories {
       mavenCentral()
       maven { url "http://artifactory.pantepic.com/artifactory/ext-sdk-rbn" }
    }
  • Then add this to your dependencies area:
    implementation 'com.ripbull.ertcsdk:coreSdk:1.0.0'
       transitive = true
    }
  • You also need to enable Java 8
    compileOptions {
       sourceCompatibility JavaVersion.VERSION_1_8
       targetCompatibility JavaVersion.VERSION_1_8
    }

Android X

Make sure you’ve added the following to your gradle.properties file.

android.useAndroidX=true

Initializing the Chat SDK

Now open your application’s main class and find the onCreate method. Add the following to set up the Chat SDK:

try {
// set your api key here
val config =
Configuration.Builder().context(this).apiKey("edu6qgwsjxxxxxApiKey").build()

// SDK initialize
eRTCSDK.initializeWithConfig(config)

} catch (Exception e) {
   // Handle any exceptions
   e.printStackTrace();
}

Sending a text message

A chat between two users is considered as a thread of communication. To initiate chat between users, you need to create threads and send messages over that thread ID.

To do that:

Creating threads for chat

Here user.id corresponds to the ID of the user you need to initiate chat with.

createThread method takes two params, one is thread name and the second is user ID.

eRTCSDK.chat().createThread("", user.id);

Send chat message

User the thread ID created above to start text messaging.

String textMessage = “This is a text message”;
eRTCSDK.chat().sendMessage(textMessage, threadID);

Block/unblock a user

To block a user: action = “block”
To unblock a user: action = “unblock”

eRTCSDK.chat().blockUnblock(action, user!!.id).subscribeOn(Schedulers.io()).observeOn(
AndroidSchedulers.mainThread()
).subscribe({
//user is blocked/unblocked depending upon the action passed

}, { it.printStackTrace() })

Viewing blocked users

eRTCSDK.chat().blockedUsers().subscribeOn(Schedulers.io()).observeOn(
AndroidSchedulers.mainThread()
).subscribe({
//check user.isBlocked to see if a particular user is blocked post this api call
}, { it.printStackTrace() })

How to handle permissions

implementation 'com.ripbull.ertcsdk:permissions:1.0.0@aar'

How to handle permissions

implementation 'com.ripbull.ertcsdk:coreAttachment:1.0.0@aar'

Sending a media message

eRTCSDK.chat().sendMedia(
Message,         //filePath
threadID,
messageRecord.msgType      //msgType = ’image’/’audio’/’video’
)

Sending a document

disposable.add(
eRTCSDK.chat().sendDocument(
   threadID, message,
   messageRecord.msgType
).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
   .subscribe({
   }, { it.printStackTrace() })
)

Sending a contact

disposable.add(
eRTCSDK.chat().sendContact(
   threadID,
   messageRecord.contactName,
   messageRecord.phoneContactRecord,
   messageRecord.emailContactRecord
).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
 .subscribe({
}, { it.printStackTrace() }))

Sending a location

disposable.add(
eRTCSDK.chat().sendLocation(
   threadID,
   messageRecord.locationRecord?.address,
   messageRecord.locationRecord?.latitude!!,
   messageRecord.locationRecord?.longitude!!
).subscribeOn(Schedulers.io()).observeOn(
      AndroidSchedulers.mainThread()
   ).subscribe({
   }, { it.printStackTrace() })
)

Sending a GIF

disposable.add(eRTCSDK.chat().sendGIF(
    threadID, message
).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
.subscribe({
   }, { it.printStackTrace() })
)

Resources

Modules:

  • Core SDK module
  • Permission module
  • Attachment module
  • Data Module
  • MQTT module
  • Analytics module
  • Core Http Module (Rest APIs)
  • Core Encryption
In-App Chat Android