Qiscus Chat SDK for Android

repository·master·Indexed 41 days ago

https://github.com/qiscus/qiscus-sdk-android

An Android library for embedding real-time 1-on-1, group, and channel chat features. Includes support for FCM push notifications, RXJava asynchronous operations, EventBus for real-time events, and secure server-side JWT authentication.

Tokens
29.2K
Snippets
86
Records
97
Agent score
52%

What's inside Qiscus Chat SDK

  1. Overview of Qiscus Chat SDK

    master

    The Qiscus Chat SDK allows developers to embed real-time chat features into Android applications without managing the underlying communication infrastructure.

    Key Capabilities:

    • Built-in UI: Provides a customizable Chat Room UI out of the box. Note that you are responsible for building your own Contact List and Conversation List UIs using the data provided by the SDK.
    • Core Features: Includes private/group chat, typing indicators, delivery/read receipts, image/file attachments, online presence, emoji support, and message deletion.
    • Advanced Extensibility: Supports server-side integration via Webhooks/Server API, bot engine embedding, push notifications, and message import/export.
  2. How Qiscus Chat SDK works

    master

    The Qiscus Chat SDK enables real-time communication through a messaging flow where users register to the Qiscus Server, open a chat room, and exchange messages.

    Key Concepts

    • Messaging Flow: Users send messages to a Chat Room. Other participants in that room receive the message via event handlers (e.g., on receive message, read receipt).
    • APP ID: A unique identifier for your application. It acts as the bridge that connects users across different platforms (iOS, Android, Web). Users within the same APP ID can communicate with each other, but users in different Qiscus applications cannot.
    • Environments: New APP IDs are initially created as trial/sandbox environments. To ensure continuous access and additional sandbox APP IDs, you may need to upgrade to a paid plan.
  3. Understand Chat Room metadata (options)

    master

    Chat Rooms in the Qiscus Chat SDK support an options parameter, which consists of string key-value pairs. This metadata is automatically synchronized across all participants in a conversation.

    Best Practices:

    • Keep the amount of data in options to a minimum to ensure fast synchronization.
    • Use it for non-critical UI or contextual data, such as setting a room's background color or storing latitude/longitude coordinates.

    Example keys/values: key: background, value: red.

  4. How EventBus and Pusher API work together

    master

    The Qiscus Chat SDK uses EventBus to broadcast real-time events (like typing indicators, message delivery, and user presence) to your application. To receive these events, you must follow a specific lifecycle:

    1. Subscribe via Pusher API: Use QiscusPusherApi.getInstance().subscribeChatRoom(roomId) or subscribeUserOnlinePresence(userId) to tell the SDK which specific entities you want to listen to.
    2. Register with EventBus: In your Activity or Fragment lifecycle (e.g., onResume), call EventBus.getDefault().register(this).
    3. Handle Events: Define methods annotated with @Subscribe to receive specific event types like QiscusCommentReceivedEvent, QiscusChatRoomEvent, or QiscusUserStatusEvent.
    4. Unsubscribe and Unregister: In onDestroy(), unsubscribe from the Pusher API and call EventBus.getDefault().unregister(this) to prevent memory leaks.
    @Override
    protected void onResume() {
        super.onResume();
        EventBus.getDefault().register(this); // Register to receive events
    }
    
    @Subscribe
    public void onReceiveRoomEvent(QiscusChatRoomEvent roomEvent) {
        // Handle TYPING, DELIVERED, READ, or CUSTOM events
    }
  5. How EventBus works in Qiscus SDK

    master

    The Qiscus SDK uses EventBus to broadcast events throughout your application. To receive events, you must register your object (e.g., an Activity or Fragment) with the EventBus and unregister it when it is no longer needed to prevent memory leaks.

    To listen for specific events, create a method annotated with @Subscribe where the parameter type matches the event class you wish to intercept.

    @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
    }
  6. Understand the core SDK components

    master

    The SDK is composed of three primary layers that handle different data and communication needs:

    1. QiscusCore: The main entry point. It manages the library lifecycle, user setup, and provides helper methods for chat rooms and configuration.
    2. QiscusApi: Handles remote interactions via HTTP requests. Use this to access server-side data like chat rooms and to send messages. Note that these methods return rx.Observable (RxJava) and do not affect local data.
    3. QiscusPusherApi: Handles real-time events via MQTT (e.g., typing indicators, message read receipts, online status). Events are dispatched via EventBus (check com.qiscus.sdk.event).
    4. QiscusDataStore: Manages local data storage. This allows for offline access and improved UX (e.g., showing cached messages immediately while fetching new ones from the server).
  7. Compare Chat Room Types

    master

    Qiscus supports three main types of chat rooms. Choose based on your use case requirements:

    Feature1-on-1GroupChannel
    Max Participants21005000
    Sent/Delivered/Read ReceiptsYesYesNo
    Push NotificationsYesYesNo
    Unread CountYesYesYes
    Block User SupportYesNoNo
    Adding/Removing ParticipantsNoYesYes
    Use CasePrivate ChatSmall GroupsForums, Live Chat
    • 1-on-1 Chat Room: Private chat between two users. Supports blocking.
    • Group Chat Room: Targeted at an array of user IDs. Supports adding/removing participants.
    • Channel: Allows users to join without invitation (e.g., public forums).
  8. How the Qiscus Event Handler works

    master

    The Qiscus Chat SDK uses EventBus (by GreenRobot) to broadcast real-time events throughout your application. This allows you to listen for events like typing indicators, message read statuses, user online status, and custom events.

    To use the event system, you must:

    1. Register your object (e.g., an Activity or Fragment) with the EventBus.
    2. Unregister the object when it is no longer needed to prevent memory leaks.
    3. Subscribe to specific events by creating methods annotated with @Subscribe where the parameter type matches the event class you want to handle.
    // Registering
    EventBus.getDefault().register(this);
    
    // Unregistering
    EventBus.getDefault().unregister(this);
  9. Use Qiscus Core SDK only

    master

    To use only the Core SDK (without the full UI features), update your build.gradle and change the initialization class from Qiscus to QiscusCore.

    dependencies {
        implementation 'com.qiscus.sdk:chat-core:1.3.4'
    }
    // Change from:
    Qiscus.init(this, "yourappid");
    
    // To:
    QiscusCore.init(this, "yourappid");
  10. Set up the Sample Dashboard locally

    master

    The Sample Dashboard is a PHP-based tool used for user management and demonstrating how to interact with the Qiscus Server API. To run it locally, you need PHP and Composer installed.

    1. Clone the repository:
      git clone https://github.com/qiscus/dashboard-sample.git
      cd dashboard-sample
    2. Install dependencies and start the server:
      composer install
      php -S localhost:8000
    3. To connect the dashboard to your own app, update the APP_ID and Secret Key inside the .env file using credentials from your Qiscus SDK dashboard.
    git clone https://github.com/qiscus/dashboard-sample.git
    cd dashboard-sample
    composer install
    php -S localhost:8000
  11. Try the Qiscus Chat SDK Sample App

    master

    To understand the full functionality and flow of the SDK, you can use the official sample application. You can customize the UI or build your own application on top of it.

    Note: The sample app uses a shared sample APP ID. If you want to test with your own data, replace it with your own APP ID from the Qiscus Dashboard.

    git clone https://bitbucket.org/qiscus/qiscus-sdk-core-android-sample.git
  12. Add metadata to comments using Extras

    master

    You can attach custom JSON data to a QiscusComment object using the setExtras method. This is useful for passing additional structured information with a message.

    Manual Assignment

    Generate a comment and then call qiscusComment.setExtras(new JSONObject(jsonString)).

    Using a Comment Interceptor

    To automatically attach extras to all outgoing messages, configure a CommentSendingInterceptor via QiscusCore.getChatConfig().setCommentSendingInterceptor(...).

    // Manual approach
    QiscusComment qiscusComment = QiscusComment.generateMessage("This is message", 123, 123);
    String stringJson = "{\"key\" : \"value\"}";
    qiscusComment.setExtras(new JSONObject(stringJson));
    
    // Interceptor approach (automatic for all messages)
    QiscusCore.getChatConfig().setCommentSendingInterceptor(qiscusComment -> {
        qiscusComment.setExtras(new JSONObject(stringJson));
        return qiscusComment;
    });