qiscus-sdk-android

qiscus-sdk-android

Android应用实时聊天SDK集成方案

qiscus-sdk-android是一款功能完善的Android聊天SDK,可为应用快速集成实时聊天功能。该SDK提供丰富API,支持一对一和群组聊天、频道、在线状态、消息回执等特性。开发者无需处理复杂的实时通信基础设施,可专注于优化用户体验。此外,SDK还支持服务器端集成和机器人引擎嵌入等高级功能。

Qiscus Chat SDKAndroid实时通信聊天功能应用开发Github开源项目

Introduction

Qiscus Chat SDK (Software Development Kit) is a product provided by Qiscus that enables you to embed an in-app chat/chat feature in your applications quickly and easily. With our chat SDK, you can implement chat feature without dealing with the complexity of real-time communication infrastructure. We provide a powerful API to let you implement chat feature into your apps in the most seamless development process.

Qiscus Chat SDK provides many features such as:

  • 1-on-1 chat
  • Group chat
  • Channel chat
  • Typing indicator
  • Image and file attachment
  • Online presence
  • Delivery receipt
  • Read receipt
  • Delete message
  • Offline message
  • Block user
  • Custom real-time event
  • Server-side integration with Server API and Webhook
  • Embed bot engine in your app
  • Enable push notification
  • Export and import messages from your app

Try Sample App

We have provided a sample app to help you get to know with our chat SDK. This sample app is built with full functionalities so that you can figure out the flow and main activities using Qiscus Chat SDK. Now you can freely customize your own UI. You can also build your own app on top of our sample app. For further details you can download this sample.

git clone https://github.com/qiscus/qiscus-chat-sdk-android-sample.git

This sample app uses sample APP ID, which means, by using this sample app, you will share the data with others. In case you want to try by your own, you can change the APP ID into your own APP ID. You can find your APP ID in your dashboard. Click here to access your dashboard

Getting Started

Step 1: Get Your APP ID

First, you need to create your application in dashboard, by accessing Qiscus Chat Dashboard. You can create more than one App ID.

Step 2: Install Qiscus Chat SDK

Qiscus Chat SDK requires minimum Android API 16 (Jelly Bean). To integrate your app with Qiscus, it can be done in 2 (two) steps. First, you need to add URL reference in your .gradle project. This reference is a guide for .gradle to get Qiscus Chat SDK from the right repository. Below is how to do that:

allprojects { 
      repositories { 
           ... 
           maven { url "https://artifactory.qiscus.com/artifactory/qiscus-library-open-source" } 
      } 
}

Second, you need to add SDK dependencies inside your app .gradle. Then, you need to synchronize to compile the Qiscus Chat SDK for your app.

dependencies { 
       ... 
       implementation 'com.qiscus.sdk:chat-core:1.8.1'
}

Step 3: Initialization Qiscus Chat SDK

You need to initiate your APP ID for your chat app before carry out to authentication. This initialization only needs to be done once in the app lifecycle. Initialization can be implemented in the initial startup. Here is how you can do that:

public class SampleApp extends Application { @Override public void onCreate() { super.onCreate(); QiscusCore.initWithAppId(this, APPID); } }

The initialization should be called once across an Android app . The best practice you can put in application class.

Step 4: Authenticate to Qiscus

To use Qiscus Chat SDK features, a user need to authenticate to Qiscus Server, for further detail you might figure out Authentication section. This authentication is done by calling setUser() function. This function will retrieve or create user credential based on the unique userId, for example:

QiscusCore.setUser(userId, userKey) .withUsername(username) .withAvatarUrl(avatarUrl) .withExtras(extras) .save(new QiscusCore.SetUserListener() { @Override public void onSuccess(QiscusAccount qiscusAccount) { //on success } @Override public void onError(Throwable throwable) { //on error });

Where:

userId (string, unique): A user identifier that will be used to identify a user and used whenever another user need to chat with this user. It can be anything, whether is is user's email, your user database index, etc. As long as it is unique and a string.

userKey (string): userKey for authentication purpose, so even if a stranger knows your user Id, he cannot access the user data.

username (string): Username for display name inside Chat Room purposes.

avatarURL (string, optional): to display user's avatar, fallback to default avatar if not provided.

You can learn from the figure below to understand what really happened when calling setUser() function:

<p align="center"><br/><img src="https://raw.githubusercontent.com/qiscus/qiscus-sdk-android/develop/screenshot/set_user.png" width="80%" /><br/></p>

Updating User Profile

After your user account is created, sometimes you may need to update a user information, such as changing user avatar. You can use method QiscusCore.updateUser() to make changes to your account.

QiscusCore.updateUser(userName, avatarUrl, new QiscusCore.SetUserListener() { @Override public void onSuccess(QiscusAccount qiscusAccount) { //do anything after it successfully updated } @Override public void onError(Throwable throwable) { //do anything if error occurs } });

Clear User Data and disconnect

As mentioned in previous section, when you did setUser(), user's data will be stored locally. When user need to disconnect from Qiscus Chat SDK service, you need to clear the user data that is related to Qiscus Chat SDK, such as token, profile, messages, rooms, etc, from local device. You can do this by calling clearUser() method :

QiscusCore.clearUser();

Create Chat Room

Chat Room is a place where 2 or more users can chat each other. There are 3 type of Chat Room that can be created using Qiscus Chat SDK: 1-on-1 Chat Room, Group Chat Room, and Channel. For some cases, a room can be identified by room unique id or room name.

1-on-1 Chat Room

We assume that you already know a targeted user you want to chat with. Make sure that your targeted user has been registered in Qiscus Chat SDK through setUser() method, as explained in the previous section. To start a conversation with your targeted user, it can be done with getChatRoom() method. Qiscus Chat SDK, then, will serve you a new Chat Room, asynchronously. When the room is succesfully created, Qiscus Chat SDK will return a Chat Room package through onSuccess() listener.

QiscusApi.getInstance().chatUser(userId, options) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(chatRoom -> { // on success }, throwable -> { // on error });

Where:

userId: A User identifier that will be used to identify a user and used whenever another user needs to chat with this user. It can be anything, whether is user's email, your user database index, etc. As long as it is unique and a string.

distinctId: (deprecated) you can fill “ ” (empty string).

options: metadata that can be as additional information to Chat Room, which consists key-value, for example, key: background, and value: red.

Group Chat Room

When you want your many users to chat together in a 1-on-1 chat room, you need to create Group Room. Basically Group Room has the same concept as 1-on-1 Chat Room, but the different is that Group Room will target array of userIds in a single method. Here how you can create Group Room:

QiscusApi.getInstance().createGroupChat(roomName, userIds, avatarUrl, options); .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(chatRoom -> { // on success }, throwable -> { // on error });

Channel

Creating Channel Chat Room is ideal for a use case that requires a lot of number participants, more than 100.

You need to set uniqueId to identify a Channel Chat Room. If a Chat Room with predefined uniqueId doesn't exist, it will create a new one with requester as the only one participant. Otherwise, if Chat Room with predefined uniqueId already exists, it will return that room and add requester as a participant.

When the room doesn't exist at the very first call and you do not send avatarUrl and/or roomName, it will use the default value. But, after the second call (room does exist) and you send avatarUrl and/or roomName, it will be updated to that value. For example the implementation creating channel:

QiscusApi.getInstance().createChannel(uniqueId, roomName, avatarUrl, options) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(chatRoom -> { // on success }, throwable -> { // on error });

Room Participant Management

In some cases, you may need to add additional participants into your room chat or even removing any participant.

Get Chat Room List

To get all room list you can call QiscusApi.getInstance().getChatRooms(int page, int limit, boolean showMembers), page start from 1, limit indicate the max rooms per page, showMembers is flag for load room members also or not. Here sample code:

QiscusApi.getInstance().getAllChatRooms(showParticipant, showRemoved, showEmpty, page, limit) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(chatRooms -> { //on success }, throwable -> { //on error });

Add Participant in Chat Room

You can add more than a participant in Chat Room by calling addRoomMember method. You can also pass multiple userIds. Once a participant succeeds to join the Chat Room, they will get a new Chat Room in their Chat Room list.

QiscusApi.getInstance().addParticipants(roomId, userId) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(chatRoom -> { // on success }, throwable -> { //on error });

Remove Participant in Chat Room

You can remove more than a participant in Chat Room by calling removeRoomMember method. You can also pass multiple userId. Once a participant is removed from the Chat Room, they will not find related Chat Room in their Chat Room list.

QiscusApi.getInstance().removeParticipants(roomId, userId) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(chatRoom -> { //success }, throwable -> { //error });

Enable Push Notification

First install FCM to your apps, you can follow this steps. You can skip this step, if your apps already use FCM. Then put your api key to qiscus dashboard. Now lets integrate with Qiscus client sdk, first enable FCM at Qiscus chat config.

Qiscus.getChatConfig().setEnableFcmPushNotification(true);

After that, you need register FCM token to notify Qiscus Chat SDK, you can call sendCurrentToken() after login and in home page (was login in qiscus), for example:

if (Qiscus.hasSetupUser()) { FirebaseUtil.sendCurrentToken(); }
public class FirebaseUtil {

    public static void sendCurrentToken() {
        AppFirebaseMessagingService.getCurrentDeviceToken();
    }
}
  • Add the .AppFirebaseMessagingService in Manifest as well, for example:
<service android:name=".AppFirebaseMessagingService"
         android:exported="true">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

Handle Incoming Message From Push Notification

After registering your FCM token, you will get data from FCM Qiscus Chat SDK, you can handle by using handleMessageReceived() method, for example:


import android.util.Log;

import androidx.annotation.NonNull;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingService;
import com.google.firebase.messaging.RemoteMessage;
import com.qiscus.sdk.chat.core.QiscusCore;
import com.qiscus.sdk.chat.core.util.QiscusFirebaseMessagingUtil;

public class AppFirebaseMessagingService extends FirebaseMessagingService {

    @Override
    public void onMessageReceived(RemoteMessage remoteMessage) {
        super.onMessageReceived(remoteMessage);

        Log.d("Qiscus", "onMessageReceived " + remoteMessage.getData().toString());
        if (QiscusFirebaseMessagingUtil.handleMessageReceived(remoteMessage)) {
            return;
        }
    }

    @Override
    public void onNewToken(@NonNull String s) {
        super.onNewToken(s);

        Log.d("Qiscus", "onNewToken " + s);
        QiscusCore.registerDeviceToken(s);
    }

    public static void getCurrentDeviceToken() {
        final String token = QiscusCore.getFcmToken();
        if (token != null) {
            FirebaseMessaging.getInstance().deleteToken()
                    .addOnCompleteListener(task -> {
                        QiscusCore.removeDeviceToken(token);
                        getTokenFcm();
                    })
                    .addOnFailureListener(e -> QiscusCore.registerDeviceToken(token));
        } else {
            getTokenFcm();
        }
    }

    private static void getTokenFcm() {
        FirebaseMessaging.getInstance().getToken()
                .addOnCompleteListener(task -> {
                    if (task.isSuccessful() && task.getResult() != null) {
                        QiscusCore.registerDeviceToken(task.getResult());
                    } else {
                        Log.e("Qiscus", "getCurrentDeviceToken Failed : " +
                                task.getException());
                    }
                });
    }
}

Event Handler

Qiscus Chat SDK provides a simple way to let applications publish and listen to some real-time events. You can publish typing, read, user status, custom event so that you can handle freely in the event handler. This lets you inform users that another participant is actively engaged in communicating with them.

Qiscus Chat SDK is using EventBus for broadcasting event to the entire applications. You can learn more about EventBus on this website. What you need to do is register the object which will receive event from EventBus. You can call it like this:

EventBus.getDefault().register(this);

Once you don't need to listen to the event anymore, you have to unregister the receiver by calling this method:

EventBus.getDefault().unregister(this);

This is an example on how to register an activity to receive event from EventBus:

public class MyActivity extends AppCompatActivity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); // listen room event QiscusPusherApi.getInstance().subscribeChatRoom(qiscusChatRoom); // listen user status QiscusPusherApi.getInstance().subscribeUserOnlinePresence("userId"); } @Override protected void onResume() { super.onResume(); EventBus.getDefault().register(this); // register to EventBus } @Override protected void onPause() { super.onPause(); EventBus.getDefault().unregister(this); // unregister from EventBus } @Subscribe public void onReceiveComment(QiscusCommentReceivedEvent event) { event.getQiscusComment(); // to get the comment } @Subscribe public void onReceiveRoomEvent(QiscusChatRoomEvent roomEvent) { switch (roomEvent.getEvent()) { case TYPING: roomEvent.getRoomId(); // this is the room id roomEvent.getUser(); // this is the qiscus user id roomEvent.isTyping(); // true if the user is typing break; case DELIVERED: roomEvent.getRoomId(); // this is the room id roomEvent.getUser(); // this is the qiscus user id roomEvent.getCommentId(); // the comment id was delivered break; case READ: roomEvent.getRoomId(); // this is the room id roomEvent.getUser(); // this is the qiscus user id roomEvent.getCommentId(); // the comment id was read break; case CUSTOM: //here, you can listen custom event roomEvent.getRoomId(); // this is the room id roomEvent.getUser(); // this is the qiscus user id roomEvent.getEventData(); //event data (JSON) break; } } @Subscribe public void onUserStatusChanged(QiscusUserStatusEvent event) { event.getUser(); // this is the qiscus user id event.isOnline(); // true if user is online event.getLastActive(); // Date of last active user } @Override protected void onDestroy() { super.onDestroy(); // stop listening room event QiscusPusherApi.getInstance().unsubsribeChatRoom(qiscusChatRoom); // stop listening user status QiscusPusherApi.getInstance().unsubscribeUserOnlinePresence("qiscus_user_id"); } }

Using Proguard

ProGuard is the most popular optimizer for Java bytecode. It makes your Java and Android applications smaller and faster. Read here for more detail about Proguard. If you are using Proguard in your application, make sure you add Proguard rules of Qiscus from Qiscus Proguard Rules to your Proguard rules.

RXJava Support

For you who prefer to code with RXJava, Qiscus Chat SDK does support RXJava. So, you can do anything as you do with Native Java. For example, to set a user, as has been explained in [Basic

编辑推荐精选

Trae

Trae

字节跳动发布的AI编程神器IDE

Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。

AI工具TraeAI IDE协作生产力转型热门
蛙蛙写作

蛙蛙写作

AI小说写作助手,一站式润色、改写、扩写

蛙蛙写作—国内先进的AI写作平台,涵盖小说、学术、社交媒体等多场景。提供续写、改写、润色等功能,助力创作者高效优化写作流程。界面简洁,功能全面,适合各类写作者提升内容品质和工作效率。

AI辅助写作AI工具蛙蛙写作AI写作工具学术助手办公助手营销助手AI助手
问小白

问小白

全能AI智能助手,随时解答生活与工作的多样问题

问小白,由元石科技研发的AI智能助手,快速准确地解答各种生活和工作问题,包括但不限于搜索、规划和社交互动,帮助用户在日常生活中提高效率,轻松管理个人事务。

热门AI助手AI对话AI工具聊天机器人
Transly

Transly

实时语音翻译/同声传译工具

Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。

讯飞智文

讯飞智文

一键生成PPT和Word,让学习生活更轻松

讯飞智文是一个利用 AI 技术的项目,能够帮助用户生成 PPT 以及各类文档。无论是商业领域的市场分析报告、年度目标制定,还是学生群体的职业生涯规划、实习避坑指南,亦或是活动策划、旅游攻略等内容,它都能提供支持,帮助用户精准表达,轻松呈现各种信息。

AI办公办公工具AI工具讯飞智文AI在线生成PPTAI撰写助手多语种文档生成AI自动配图热门
讯飞星火

讯飞星火

深度推理能力全新升级,全面对标OpenAI o1

科大讯飞的星火大模型,支持语言理解、知识问答和文本创作等多功能,适用于多种文件和业务场景,提升办公和日常生活的效率。讯飞星火是一个提供丰富智能服务的平台,涵盖科技资讯、图像创作、写作辅助、编程解答、科研文献解读等功能,能为不同需求的用户提供便捷高效的帮助,助力用户轻松获取信息、解决问题,满足多样化使用场景。

热门AI开发模型训练AI工具讯飞星火大模型智能问答内容创作多语种支持智慧生活
Spark-TTS

Spark-TTS

一种基于大语言模型的高效单流解耦语音令牌文本到语音合成模型

Spark-TTS 是一个基于 PyTorch 的开源文本到语音合成项目,由多个知名机构联合参与。该项目提供了高效的 LLM(大语言模型)驱动的语音合成方案,支持语音克隆和语音创建功能,可通过命令行界面(CLI)和 Web UI 两种方式使用。用户可以根据需求调整语音的性别、音高、速度等参数,生成高质量的语音。该项目适用于多种场景,如有声读物制作、智能语音助手开发等。

咔片PPT

咔片PPT

AI助力,做PPT更简单!

咔片是一款轻量化在线演示设计工具,借助 AI 技术,实现从内容生成到智能设计的一站式 PPT 制作服务。支持多种文档格式导入生成 PPT,提供海量模板、智能美化、素材替换等功能,适用于销售、教师、学生等各类人群,能高效制作出高品质 PPT,满足不同场景演示需求。

讯飞绘文

讯飞绘文

选题、配图、成文,一站式创作,让内容运营更高效

讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。

热门AI辅助写作AI工具讯飞绘文内容运营AI创作个性化文章多平台分发AI助手
材料星

材料星

专业的AI公文写作平台,公文写作神器

AI 材料星,专业的 AI 公文写作辅助平台,为体制内工作人员提供高效的公文写作解决方案。拥有海量公文文库、9 大核心 AI 功能,支持 30 + 文稿类型生成,助力快速完成领导讲话、工作总结、述职报告等材料,提升办公效率,是体制打工人的得力写作神器。

下拉加载更多