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.
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 a 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:
let message = CommentModel()
message.message = "textComment"
message.type = "text"
message.roomId = roomId // you can get this id in chat room object
message.extras = metadata
QiscusCore.shared.sendMessage(message: message, onSuccess: { (commentModel) in
//success
}) { (error) in
print(error.message)
}
Where:
message
(object): consist ofmessage
,type
, androomId
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 and custom your structured data, you may face your structured data will not work, these type for bot API, hence you need define other type name.extras
([string:any], 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:
///set your delegate in viewWillAppear
func setRoomDelegage(){
if let room = self.room {
room.delegate = self
}
}
//remove your delegate in viewWillDisappear
func removeRoomDelegate() {
if let room = self.room {
room.delegate = nil
}
}
extension YourChatViewController : QiscusCoreRoomDelegate {
// MARK: Message event in a chat room
/// new message is comming
///
/// - Parameters:
/// - comment: new comment object
func onRoomMessageReceived(comment: CommentModel){
}
}
Get Thumbnail URL
You can retrieve the media URL or the thumbnail URL, by following this code:
QiscusCore.shared.getThumbnailURL(fileUrl)
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:
let message = CommentModel()
message.message = "Hi"
message.type = "yourtype"
message.payload = yourPayload
message.roomId = roomId
QiscusCore.shared.sendMessage(message: message, onSuccess: { (commentModel) in
//success
}) { (error) in
print(error.message)
}
Where:
message
(object): consist ofmessage
,type
, androomId
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 and custom your structured data, you may face your structured data will not work, these type for bot API, hence you need define other type name.payload
([string:any], optional) : payload for defining the structured message data, for example you want to create your own file message, you can fill the payload 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 didComment event section.
You can update message read status by calling markAsRead()
method, for example:
/// Mark Comment as read, include message before
QiscusCore.shared.markAsRead(roomId: roomId, commentId: lastComment.id)
Update message read status only work on 1-on-1 chat and group chat
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
QiscusCore.shared.getPreviousMessagesById(roomID: roomId, limit: limit, messageId: messageId, onSuccess: { (comments) in
print("success")
}) { (error) in
print("error =\(error.message)")
}
Where:
roomId
(string): chat room id, you can get this id in chat room objectlimit
(number, optional): number of messages, max 100 data, by default is 20 messagesmessageId
(string): starting pointer to get previous messages, you can get this id in message object
You can a get message from local data, for example:
if let comments = QiscusCore.database.comment.find(id: "roomId"){
//success
}
Upload File (% of Process)
You can send a raw file by passing file
. In return you will URL and progressEvent, that you can use this listener to create your own uploading process UI, for example::
let file = FileUploadModel()
file.data = data
file.name = imageName
QiscusCore.shared.upload(file: file, onSuccess: { (fileURL) in
print("success")
}, onError: { (error) in
print("error \(String(describing: error?.message))")
}) { (progress) in
print("progress \(progress)")
}
Download Media (The Path and % of Process)
You can download a file by passing url
. In return, you will get progress listener. You can use this listener to create your own downloading process UI, for example:
QiscusCore.shared.download(url: url, onSuccess: { (urlFile) in
print("success")
}) { (progress) in
print("progress \(progress)")
}
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:
QiscusCore.shared.deleteMessages(messageUniqueIds: messageUniqueIds, onSuccess: { (comments) in
print("success")
}) { (error) in
print("error \(String(describing: error.message))")
}
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:
QiscusCore.shared.clearMessagesByChatRoomId(roomUniqeIds: roomUniqeIds) { (error) in
print("error \(error?.message)")
}
The maximum number of roomUniqueIds for an API call is 10. If you pass more than 10 roomUniqueIds it returns an error