Before you begin
- Register a QuickBlox account. This is a matter of a few minutes and you will be able to use this account to build your apps.
- Configure QuickBlox SDK for your app. Check out Setup page for more details.
- Create a user session to be able to use QuickBlox functionality. See Authentication page to learn how to do it.
- Connect to the Chat server. See Connection page to learn how to do it.
- Create a dialog. See Dialogs page to learn how to do it.
Subscribe message events
To add the listener to receive messages in real-time, use theaddDialogMessageListener() method. The listener enables the app to listen to the incoming message.
- Java
- Kotlin
- Java
- Kotlin
Send text message
To send a message to a private, group or public dialog, use the code snippet below.- Java
- Kotlin
You need to join the group and public dialog by calling the
join() method before you start chatting in a dialog. Once the dialog is joined, you can receive/send messages. See this section to learn how to join the dialog.- Synchronous.
- Java
- Kotlin
- Asynchronous.
- Java
- Kotlin
Send message with attachment
Chat attachments are supported by the content API. In order to send a chat attachment, you need to upload the file to QuickBlox cloud storage and obtain a link to the file (file UID). Then you need to include this UID into the chat message and send it.- Java
- Kotlin
- Java
- Kotlin
Send message with extra data
You have an option to extend the message with additional fields. Specify one or more key-value items in themessage. Using these items, you can implement the ability for a user to send self-location information to another user or notification messages signifying that a user has left a group, etc.
- Java
- Kotlin
Set the following fields of the
qbChatMessage:
Retrieve chat history
Every dialog stores its chat history that you can retrieve using thegetDialogMessages() method. The request below will return messages for a specific dialog limited to the 100 dialogs on the page.
- Java
- Kotlin
If you want to mark all retrieved chat messages as a read, set the
markAsRead parameter as true. If you decide not to mark chat messages as read, just set markAsRead parameter as false.QBMessageGetBuilder class.
If you want to get a paginated list of messages from the server, you can set the following pagination parameters in the QBMessageGetBuilder class.
Search operators
You can use search operators to search for the exact data that you need.Sort operators
You can use sort operators to order the search results.Update message
To update a message text , use theupdateMessage() method below.
- Java
- Kotlin
Delete message
Any user in theoccupantIDs can delete a message from the dialog. As a result, the message will be deleted from the current user history, without affecting the histories of other users.
The owner of the dialog can completely remove messages from all users’ histories. This is achieved by setting the forceDelete parameter to true.
- Java
- Kotlin
Check if a message is sent
The message is considered as sent if it has been delivered to the server. To get to know that a message has been delivered to the server, make sure to enable a stream management before connecting to the Chat server. See this section to learn how to enable the stream management. Thus, to track the event when your message becomes sent, use theQBChatDialogMessageSentListener.
- Java
- Kotlin
Mark message as delivered
As a sender, you may want to be informed that a message has been successfully delivered to the recipient. The mark-as-delivered functionality allows to notify the sender about message delivery. To track the event when the message has been delivered to the user, use theQBMessageStatusListener().
- Java
- Kotlin
deliverMessage() method to mark a message as delivered. As a result, the server will notify a sender about the delivery receipt.
- Java
- Kotlin
markable as true using the sendMessage() method if you want, as a sender, to receive message delivery receipts from other recipients. Thus, the markable parameter enables the sender to request the delivery receipt. It also enables the recipient to confirm the message delivery. However, if markable is false or omitted, then you can notify a sender about the delivery receipt using the deliverMessage() method.
- Java
- Kotlin
Mark message as read
As a sender, you may want to be informed that a message has been read by the recipient. The mark-as-read functionality allows to notify the sender that a message has been read. To track the event when the message has been read by the user, use theQBMessageStatusListener().
- Java
- Kotlin
readMessage() method to mark a message as read. As a result, the server will notify a sender about the read receipt.
- Java
- Kotlin
Send typing indicators
You may want, as a sender, to let the recipient know that you are typing the message or have stopped typing the message. Use typing indicators as a form of chat-specific presence. Typing indicators allow to indicate if users are typing messages in a dialog at the moment. There are the following typing notifications supported.- typing. The user is composing a message. The user is actively interacting with a message input interface specific to this chat session (for example, by typing in the input area of a chat window).
- stopped. The user had been composing but now has stopped. The user has been composing but has not interacted with the message input interface for a short period of time (for example, 30 seconds).
QBChatDialogTypingListener().
- Java
- Kotlin
QBChatDialogTypingListener().
- Java
- Kotlin
sendIsTyping() method.
- Java
- Kotlin
sendStopTypingNotification() method.
- Java
- Kotlin
Send system messages
There is a way to send system messages to other users about some events. For example, a system message can be sent when a user has joined or left a group dialog. These messages are handled over a separate channel and are not be mixed up with regular chat messages. Thus, they are handled by theQBSystemMessageListener listener. See how to add the listener in the snippet below.
System messages are also not shown in the dialog history and, consequently, are not stored on the server. This means that these messages will be delivered only to online users. Send system messages using the sendSystemMessage() method.
- Java
- Kotlin
Set the following fields of the
qbChatMessage: