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 3 statues, which are sent, delivered, 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 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 Message
You can send a text message or custom message type. 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, ticket concert message, a product info, and others UI messages that need to be customized. You need to create CommentModel object first before sending it, for example:
Create CommentModel object, text type:
let message = CommentModel()message.message = "textComment"message.type = "text"QiscusCore.shared.sendMessage(roomID: roomId, comment: message, onSuccess: { (commentModel) in //success}) { (error) in print(error.message)}Create CommentModel object, custom type:
let message = CommentModel()message.message = "textComment"message.type = "yourtype"message.payload = yourPayloadWhere:
- 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, replay, 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.
- payload: payload to define 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"}You can find how to implement this content in Sample. Another example content you can craft:
{ "cards": [ { "header": { "title": "Pizza Bot Customer Support", "subtitle": "[email protected]", "imageUrl": "https://goo.gl/aeDtrS", "imageStyle": "IMAGE" }, ... } ]}You can add extras before sending a message, for example
let extraData : [String: Any] =[ "data": [ "latitude" : 9091234123, "longtitue": -9091234123, ] ]let message = CommentModel()message.message = "textComment"message.type = "yourtype"message.extras = extraDataMetadata is automatically synchronized by each participant in the Chat Room, it is important that the amount of data stored in metadata is kept to a minimum to ensure the quickest synchronization possible.
Secondly, you can send a message using sendMessage method and need a CommentModel as parameter, for example:
QiscusCore.shared.sendMessage(roomID: roomId, comment: message, onSuccess: { (commentModel) in //success}) { (error) in print(error.message)}Receive Message
When user successfully send a message, other participant receive the message through gotNewComemnt event. You need to implement this event for getting this message, for example:
func gotNewComment(comment: CommentModel){ //got new message }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 send read status, other participants will receive a read event, for further detail you can refer to this section didComment event section.
You can update message read status by calling updateCommentRead() method, for example:
/// Mark Comment as read, include comment beforeQiscusCore.shared.updateCommentRead(roomId: roomId, lastCommentReadId: commentId)Update message read status only work on 1-on-1 Chat and Group Chat.
Load Message with Limit and Offset
You can get previous messages by calling loadComments method, by default you get 20 messages start from your lastCommentId, and also you can use this for load more the older messages, for example:
QiscusCore.shared.loadComments(roomID: roomID, lastCommentId: lastCommentId, limit: 20, onSuccess: { (comments) in print("success") }) { (error) in print("error =\(error.message)")}Where:
- roomId: ChatRoom Id
- lastComemntId: messageId that you can get from commentModel 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 upload a file by passing data and fileName. In return, you will get URI and progress listener. You can use this listener to create your own uploading process UI, for example:
QiscusCore.shared.upload(data: data, filename: fileName, onSuccess: { (fileModel) 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 deleteMessage method for example:
QiscusCore.shared.deleteMessage(uniqueIDs: uniqueIDs, onSuccess: { (comments) in print("success") }) { (error) in print("error \(String(describing: error.message))") }Clear All Messages in Particular Chat Room
You can clear all message by passing array of roomId or roomUniquesIds. This function will clear all messages only effect on QiscusAccount side, other participants still remain. For example:
QiscusCore.shared.deleteAllMessage(roomID: roomsID) { (error) in print("error \(error?.message)")}Qiscus Technology