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 3 statues, which are sent, delivered, read for a message. Once message is sent, the onMessageReceived
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 extras. 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 chat room 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:
String message = "Hi"
//Generate message object
QiscusComment qiscusMessage = QiscusComment.generateMessage(roomId, message)
//Send message
QiscusApi.getInstance().sendMessage(qiscusMessage)
.subscribeOn(Schedulers.io()) // need to run this task on IO thread
.observeOn(AndroidSchedulers.mainThread()) // deliver result on main thread or UI thread
.subscribe(qiscusChatRoom -> {
// on success
}, throwable -> {
// on error
});
Where:
roomId
(long): chat room id, you can get this id in chat room objectmessage
(string): text that you send to other participanttype
(string): message type, that you can define freely, there are reserved rich messages type, for example: text, file_attachment, account_linking, buttons, button_postback_response, reply, system_event, card, custom, location, contact_person, carousel. These type have been taken, if you use it you may face your structured data will not work, these type for bot API, hence you need define other type name.extras
(object, optional): to give additional information (metadata) to user, which consist key-value, for example key:position, and value: engineer
After successfully send a message other participant get the message through onMessageReceived
event.
Text message type can contain metadata
Receiving Message
When user successfully send a message, other participant receive the message through onMessageReceived
event. You need to implement this event for getting this message, for example:
public 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.
Get Thumbnail URL
You can retrieve the media URL or the thumbnail URL, by following this code:
QiscusFileUtil.getThumbnailURL(Url);
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 concert ticket message, a product info, and others UI messages that need to be customized. You need to create message object first before sending it, for example:
//Generate message object
QiscusComment qiscuscomment = QiscusComment.generateCustomMessage(roomId, text, type, payload);
//Send message
QiscusApi.getInstance().postComment(qiscusComment)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(commentSent -> {
// success
}, throwable -> {
// error
});
Where:
roomId
(long): chat room id, you can get this id in chat room objectmessage
(string): text that you send to other participanttype
(string): message type, that you can define freely, there are reserved rich messages type, for example: text, file_attachment, account_linking, buttons, button_postback_response, reply, system_event, card, custom, location, contact_person, carousel. These type have been taken, if you use it you may face your structured data will not work, these type for bot API, hence you need define other type name.payload
(JSON) : 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"
}
Mark Message as Read
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 messageId
.
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 markAsRead()
method, for example:
QiscusPusherApi.getInstance().markAsRead(roomId, messageId)
.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 send a raw file by passing file
. In return you will URI and progressEvent, that 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
});
Get Previous Messages
You can get previous messages by calling getPreviousMessagesById()
method, by default you get 20 messages start from your messageId
, and also you can use this to load more the older messages, for example:
QiscusApi.getInstance().getPreviousMessagesById(roomId, limit, messageId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(comments -> {
// success
}, throwable -> {
// error
});
Where:
roomId
(long): chat room id, you can get this id in chat room objectlimit
(int, optional): number of messages, max 100 data, by default is 20 messagesmessageId
(long, optional): starting pointer to get previous messages, you can get this id in message 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, 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 deleteMessages()
method, you need to pass array of messageUniqueIds.
You can get messageUniqueId
in message object. Below code is example to delete message:
QiscusApi.getInstance().deleteMessages(messageUniqueId)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(file -> {
//on success
}, throwable -> {
//on error
});
Clear All Messages in Particular Chat Room
You can clear all messages by passing array of roomUniqueIds
. This clear all messages API only effect QiscusAccount
side, other participants still have their messages. Below code is example to clear all messages:
QiscusApi.getInstance().clearMessagesByChatRoomUniqueIds(roomUniqueIds)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(voids -> {
// on success
}, throwable -> {
// on error
});
The maximum number of roomUniqueIds for an API call is 10. If you pass more than 10 roomUniqueIds it returns an error
And optionally, you can delete from your local data as well, for example:
QiscusCore.getDataStore().deleteCommentsByRoomId(roomId)