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:
//Generate message object
var message = qiscus.generateMessage(
text: text,
chatRoomId: roomId,
sender: senderUser,
extras: <String, dynamic>{"key": "value"}
);
//Send message
qiscus.sendMessage(
message: message,
).then((message) {
print('success ${message}');
});
Where:
roomId
(long): chat room id, you can get this id in chat room objecttext
(string): text that you send to other participantsender
(QUser): QUser object consist of below properties, such as:
QUser Property | Description |
---|---|
id | sender userId, you can get this value once you are successfully login in Qiscus Server |
name | sender name, you can get this value once you are successfully login in Qiscus Server |
avatarUrl | sender avatarUrl, you can get this value once you are successfully login in Qiscus Server |
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:
qiscus.onMessageReceived().listen((message) {
print('new message $message');
});
Send File 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 file attachment type:
//Generate message object
var messageFile = qiscus.generateFileAttachmentMessage(
chatRoomId: 1,
text: 'something',
caption: 'some caption',
url: 'https://via.placeholder.com/200',
);
//Send message
qiscus.sendFileMessage(message: message, file: File)
.listen((event) {
var message = event.data;
var progress = event.progress;
print('progress: $progress');
print('message: $message');
});
Where:
roomId
(long): chat room id, you can get this id in chat room objecttext
(string): text that you send to other participantsender
(QUser): QUser object consist of below properties, such as:
QUser Property | Description |
---|---|
id | sender userId, you can get this value once you are successfully login in Qiscus Server |
name | sender name, you can get this value once you are successfully login in Qiscus Server |
avatarUrl | sender userId, you can get this value once you are successfully login in Qiscus Server |
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.
Get Thumbnail URL
You can retrieve the media URL or the thumbnail URL, by following this code:
qiscus.getThumbnailURL(URL); // return thumbnail URL
Send Custom Message (on going)
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:
var customMessage = qiscus.generateCustomMessage(
chatRoomId: 1,
text: 'Custom text here',
type: 'custom message type',
payload: <String, Object?>{
'custom payload key': 'custom payload value',
},
);
qiscus.sendMessage(message: message);
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:
qiscus.markAsRead(
roomId: roomId,
messageId: messageId,
).then((_) {
print('success');
});
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 progress event, that you can use this listener to create your own uploading process UI, for example:
var cancelToken = CancelToken(); // Optional
var stream = qiscus.upload(file, cancelToken: cancelToken);
stream.listen((event) {
var url = event.data;
var progress = event.progress;
print('url: $url');
print('progress: $progress');
});
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:
qiscus.getPreviousMessagesById(
roomId: roomId,
limit: limit,
messageId: messageId,
).then((messsages) {
print('success ${messages}');
});
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): starting pointer to get previous messages, you can get this id in message object
Get Next Messages
You can get later messages than the given messageId
, by calling getNextMessagesById()
method, by default you get 20 messages start from your messageId
, for example:
qiscus.getNextMessagesById(
roomId: roomId,
limit: limit,
messageId: messageId,
).then((messages) {
print('success ${messages}');
});
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): starting pointer to get next messages, you can get this id in message object
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:
qiscus.deleteMessages(
messageUniqueIds: messageUniqueIds,
).then((messages) {
print('success ${messages}');
});
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:
qiscus.clearMessagesByChatRoomId(
roomUniqueIds: roomUniqueIds,
).then((_) {
print('success');
});
The maximum number of roomUniqueIds for an API call is 10. If you pass more than 10 roomUniqueIds it returns an error
Generate Message
Text
var message = qiscus.generateMessage(
roomId: 123,
text: "Some text",
extras: { key: "value" },
)
File Attachment
var message = qiscus.generateFileAttachmentMessage(
roomId: 123,
text: 'Something',
url: 'https://via.placeholder.com/200',
filename: '200.jpg',
size: 200,
caption: 'Some caption',
extras: { key: 'value'}
)
Custom
var message = qiscus.generateCustomMessage(
roomId: 123,
text: 'Some text',
type: 'my-custom-type',
payload: { key: 'value' },
extras: { key: 'value' }
)
Sending Generated Message
qiscus.sendMessage(
message: message,
).then((message) {
print('success ${message}');
});
roomId | (number) Which room does this message belong to |
text | (string) A string which will shown to user (for message with "text" type) |
extras | (json) A custom additional data for this specific message |
url | (string) A URL to which the file attachment refer into (for message with "file attachment" type) |
filename | (string) File filename (for message with "file attachment" type) |
size | (number) File size in byte (for message with "file attachment" type) |
caption | (string) A string which will be shown to user (for message with "file attachment" type) |
payload | (json) A custom data to represent your custom message (for message with "custom" type) |