Invite users to channels

Requires App Context

To manage channel membership, you must enable App Context for your app's keyset in the Admin Portal.

Let users invite one or more people to a private or group conversation and sets their channel membership.

As a result or inviting one or more users to a direct or group channel, an event of the invite type gets created. You can listen to these events in your chat app and notify the invited users.

Not available for public chats

Invitations are disabled in public chats. If you try implementing this feature in a public channel type, you'll get the Channel invites are not supported in Public chats error.

Chat SDK doesn't provide any default logic that is triggered when one user invites another user to join a channel. However, if you want the invited user to receive a notification about the new invitation, you can implement custom logic in your chat app that will:

Invite one user

invite() requests another user to join a channel and become its member.

Method signature

This method takes the following parameters:

channel.invite(user: User): PNFuture<Membership>

Input

ParameterTypeRequiredDefaultDescription
userUserYesn/aUser that you want to invite to a 1:1 channel. Possible only in Direct and Group channels.

Output

TypeDescription
PNFuture<Membership>Returned (modified) object containing the membership data.

Basic usage

Invite support-agent-15 to join the high-prio-incidents channel.

// assuming you have a chat service or a chat client object called "chat"
val userId = "support-agent-15"
val channelId = "high-prio-incidents"
// reference "support-agent-15"
chat.getUser(userId).async { userResult ->
userResult.onSuccess { user ->
if (user != null) {
// User found, proceed to get the "high-prio-incidents" channel
chat.getChannel(channelId).async { channelResult ->
channelResult.onSuccess { channel ->
if (channel != null) {
// Channel found, proceed to invite the agent to join the channel
channel.invite(user).async { inviteResult ->
inviteResult.onSuccess { membership ->
// User successfully invited
show all 30 lines

Invite multiple users

inviteMultiple() requests other users to join a channel and become its members. You can invite up to 100 users at once.

Method signature

This method takes the following parameters:

channel.inviteMultiple(users: Collection<User>): PNFuture<List<Membership>>

Input

ParameterTypeRequiredDefaultDescription
usersCollection<User>Yesn/aList of users you want to invite to the group channel. You can invite up to 100 users in one call.

Output

TypeDescription
PNFuture<List<Membership>>Returned (modified) list of objects containing the membership data.

Basic usage

Invite support-agent-15 and support-agent-16 to join the high-prio-incidents channel.

// assuming you have a chat service or a chat client object called "chat", reference both agents and the "high-prio-incidents" channel
val userId1 = "support-agent-15"
val userId2 = "support-agent-16"
val channelId = "high-prio-incidents"

chat.getUser(userId1).async { userResult1 ->
userResult1.onSuccess { user1 ->
if (user1 != null) {
// First user found, now get the second user
chat.getUser(userId2).async { userResult2 ->
userResult2.onSuccess { user2 ->
if (user2 != null) {
// Second user found, now get the channel
chat.getChannel(channelId).async { channelResult ->
channelResult.onSuccess { channel ->
show all 40 lines

Listen to invite events

As an admin of your chat app, you can use the listenForEvents() method to monitor all events emitted when someone invites a person to a direct or group channel. You can use this method to send notifications to the invited users or to any selected channel.

Events documentation

To read more about the events of type invite, refer to the Chat events documentation.

Method signature

This method has the following parameters:

inline fun <reified T : EventContent> listenForEvents(
channel: String,
customMethod: EmitEventMethod?,
noinline callback: (event: Event<T>) -> Unit
): AutoCloseable {
return listenForEvents(T::class, channel, customMethod, callback)
}
Input
ParameterTypeRequiredDefaultDescription
Treified T : EventContentYesn/aReified type parameter bounded by the EventContent interface, allowing access to type information at runtime.
channelStringYesn/aChannel to listen for new invite events. It can be either the user's channel (ID of the invited user) or any other channel, depending where you want to send invite events.
customMethodStringNon/aAn optional custom method for emitting events. If not provided, defaults to null.
callbacknoinline (event: Event<T>) -> UnitYesn/aA lambda function that is called with an Event<T> as its parameter. It defines the custom behavior to be executed whenever an invite event type is detected on the specified channel.
Output
TypeDescription
AutoCloseableInterface that lets you stop receiving invitation-related updates (invite events) by invoking the close() method.

Basic usage

Print a notification for an invitation received on the userId channel.

chat.listenForEvents("userId") { event: Event<EventContent.Invite> ->
println("Notification: Received an invite for userId: '${event.payload.channelId}'")
}
Last updated on