This is the new Docs for Chat SDK v4 for Android. To see the previous Docs, click here.
Create a poll
The polls feature allows group channel members and channel operators to create and send a poll attached to text messages. A poll usually consists of a question and at least one poll option that users can vote on. The functionality provides an easier way to gather feedback from groups of all sizes, collect data from customers, and drive user engagement. You can configure various settings for your poll, including when the poll will close and whether to allow users to add poll options or vote on multiple poll options.
Prerequisite
To use polls in your Sendbird application, you must activate the polls feature on Sendbird Dashboard. Go to Settings > Chat > Features and turn on Polls.
Limitations
Refer to the following limitations when using polls.
Polls can't be sent in the form of following message types: file messages, admin messages, and scheduled text messages.
Polls are only available in group channels except for ephemeral and Supergroup channels. See channel types.
Data on polls isn't included in the result when exporting message data.
Note: The maximum number of options that can be added to a poll differs depending on your Sendbird subscription plan. For further information, contact our sales team.
PollCreateParams
You can create a poll by creating and passing a PollCreateParams object as an argument to the parameter in the create() method.
val params = PollCreateParams(
title = "Title",
optionTexts = listOf("Option 1", "Option 2"),
data = PollData("Additional data"),
allowUserSuggestion = false,
allowMultipleVotes = false,
closeAt = -1 // If you don't want to specify a closing time, then pass -1.
)
Poll.create(params) { poll, e ->
if (e != null) {
// Handle error.
}
// After creating a poll, the poll is sent to the channel.
if (poll != null) {
val userMessageParams = UserMessageCreateParams("Poll").apply {
pollId = poll.id
}
channel.sendUserMessage(userMessageParams) { message, e ->
if (e != null) {
// Handle error.
}
}
}
}
List of parameters
Parameter name
Type
Description
title
string
Specifies the title of a poll.
optionTexts
Array of strings
Specifies the texts of possible options for which a user can vote on. Note that this property is only valid when creating a poll, but is ignored when updating a poll.
data
PollData
Specifies an additional data to accompany the poll. A use case might be to provide explanations for incorrect quiz answers.
allowUserSuggestion
Boolean
Determines whether to allow users to make suggestions. (Default: false)
allowMultipleVotes
Boolean
Determines whether to allow users to vote on more than one poll options. (Default: false)
closeAt
Long
Specifies the time when a poll has closed or will close in Unix seconds. If the value of this property is -1, the poll status remains open meaning that the poll will never close.
PollHandler
Through PollHandler, the Sendbird server always notifies whether your poll has been successfully created.
fun interface PollHandler {
fun onResult(poll: Poll?, e: SendbirdException?)
}