Title
Create new category
Edit page index title
Edit category
Edit link
Message
You can send a message in a chat room, and other user get the message, push notification, and unread count as long as they are participant in that chat room.
Qiscus Chat SDK has 2 statues, which are sent, read for a message. Once message is sent, the OnReceiveMessage event handler will be called. To understand this part, you can refer to Event Handler section.
Sent, delivered, and read receipt in a message only work on 1-on-1 Chat and Group Chat. Hence, you may not get these message receipts in a Channel type.
In Message, you can add metadata called content. The extras is automatically synchronized by each participant in a Chat Room.
Sent, delivered, and read receipt in a message only work on 1-on-1 Chat and Group Chat.
Send Text Message
You can send a text message by passing the roomId. You can get roomId from QiscusChatRoom object that you have created or participated for 1-on-1, Group Chat or Channel.
Below is a detailed example of how to send a message using text type:
- Generate QiscusComment object, and fill the value, roomId and the message:
QiscusComment qiscusComment = QiscusComment.generateMessage(roomId, text)2. Send message by passing the qiscusComment object that has created, for example:
QiscusApi.getInstance().postComment(qiscusComment) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(commentSent -> { // success }, throwable -> { // error });After successfully send a message other participant get the message through OnReceiveMessageevent.
Text message type can contain metadata
Receive Message
When user successfully send a message, other participant receive the message through OnReceiveMessage event. You need to implement this event for getting this message, for example:
@Subscribepublic void onReceiveComment(QiscusCommentReceivedEvent event) { event.getQiscusComment(); // to get the comment}This event using eventBus, for further detail using eventBus you can see this event handler section.
Send Custom Message
The custom message is the best option to create customized UI message needs by sending structured data, such as when you need to send location message, a ticket concert message, a product info, and others UI messages that need to be customized. You need to create QiscusComment object first before sending it, for example:
Generate QiscusComment object, custom type:
QiscusComment qiscuscomment = QiscusComment.generateCustomMessage(roomId, text, type, content);Where:
- roomId: Chat Room Identity (Id). You can get this Id in QiscusChatRoom object,
- text: message text that you send to other participants,
- type: message type that you can define based your needs. There are reserved rich message types, for example, text, file_attachment, account_linking, buttons, button_postback_response, reply, system_event, card, custom, location, contact_person, and carousel. These types have been taken by Qiscus. So, if you use them, you may face a situation where your structured data will not work. These types are taken for Chat Server API, hence you need to define other type names.
- content: payload to define the structured message data. For example, you want to create your own file message, you can fill the content using this example JSON:
{ "url": "https://d1edrlpyc25xu0.cloudfront.net/sampleapp-65ghcsaysse/docs/upload/2sxErjgAfp/Android-Studio-Shortcuts-You-Need-the-Most-3.pdf", "caption": "", "file_name": "Android-Studio-Shortcuts-You-Need-the-Most.pdf"}Metadata is automatically synchronized by each participant in the Chat Room. It is important that the amount of data stored in metadata is kept in a minimum to ensure the quickest synchronization possible.
Second, you can send a message using postComment method and you need a QiscusComment as a parameter, for example:
QiscusApi.getInstance().postComment(qiscusComment) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(commentSent -> { // success }, throwable -> { // error });Update Message Read Status
You can set your message status into read. The ideal case of this, is to notify other participants that a message has been read. You need to pass roomId and commentId.
When you have 10 messages, let's say it has message Ids from 1 to 10 and put 10 as the latest message Id. Once you set read message status with the latest message, in this case is 10, your previous messages which are 1 to 9 message Ids, will be updated into read and it will make your unread count become 0.
After succeed to send read status, other participants will receive a read event, for further details you can refer to Event Handler section .
You can update message read status by calling setUserRead method, for example:
QiscusPusherApi.getInstance().setUserRead(roomId, commentId) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(void -> { // success }, throwable -> { // error });Update message read status only work on 1-on-1 Chat and Group Chat.
Upload File (% of Process)
You can upload a file by passing file. In return, you will get URI and progress listener. You can use this listener to create your own uploading process UI, for example:
QiscusApi.getInstance().uploadFile(file, progress -> { // here you can get the progress total file uploaded }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(uri -> { // on success get Uri }, throwable -> { // on error });Load Message with Limit and Offset
You can get previous messages by calling getComments method. By default, you will get 20 messages started from your lastCommentId. You can also use this to load more the older messages, for example:
QiscusApi.getInstance().getComments(roomId, lastCommentId) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(comment -> { // success }, throwable -> { // error });Where:
- roomId: ChatRoom Id,
- lastCommentId: messageId that you can get from QiscusComment object.
Download Media (The Path and % of Process)
You can download a file by passing url and fileName. In return, you will get file and progress listener. You can use this listener to create your own downloading process UI, for example:
QiscusApi.getInstance().downloadFile(url, fileName, totalDownloaded -> { // here you can get the progress total file downloaded });That code only downloads the file without saving information to local data. To save data into local data, you can use this example:
QiscusApi.getInstance() .downloadFile(url, filename, total -> { // here you can get the progress total downloaded }) .doOnNext(file -> { // here we update the local path of file QiscusCore.getDataStore() .addOrUpdateLocalPath(qiscusComment.getRoomId(), qiscusComment.getId(), file.getAbsolutePath()); }) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(file -> { //on success }, throwable -> { //on error });Delete Message
You can delete a message by calling this deleteComments method, for example:
QiscusApi.getInstance().deleteComments(commentUniqueIds, isHardDelete);Where:
- commentUniqueIds: uniqueId in QiscusComment object,
- isHardDelete: it will be deprecated soon. But at this moment, you can set true to make your messages gone in the local data, and set false to remain your messages in the local database.
Clear All Messages in Particular Chat Room
You can clear all messages by passing array of roomIds . This function will clear all messages and affected only on QiscusAccount side, while other participants will still remain. For example:
QiscusApi.getInstance().clearCommentsByRoomIds(roomIds) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(voids -> { // on success }, throwable -> { // on error });And optionally, you can delete from your local data as well, for example:
QiscusCore.getDataStore().deleteCommentsByRoomId(roomId)Qiscus Technology