On this page

Invite users to channels

Requires App Context

Enable App Context for your keyset in the Admin Portal.

Invite users to private or group conversations, creating their channel membership. Invitations trigger an invite event that you can listen to and notify invited users.

To send notifications on invitations, implement custom logic to:

Invite one user

Request a user to join a channel with invite().

Method signature

This method takes the following parameters:

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

Input

* required
ParameterDescription
user *
Type: User
Default:
n/a
User that you want to invite.

Output

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

Sample code

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

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

Invite multiple users

Request multiple users to join a channel with inviteMultiple(). Maximum 100 users per call.

Method signature

This method takes the following parameters:

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

Input

* required
ParameterDescription
users *
Type: Collection<User>
Default:
n/a
List 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.

Sample code

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

Monitor invitation events with listenForEvents() and send notifications to invited users.

Events documentation

See Chat events for details on invite events.

Method signature

This method has the following parameters:

1inline fun <reified T : EventContent> listenForEvents(
2 channel: String,
3 customMethod: EmitEventMethod?,
4 noinline callback: (event: Event<T>) -> Unit
5): AutoCloseable {
6 return listenForEvents(T::class, channel, customMethod, callback)
7}
Input
* required
ParameterDescription
T *
Type: reified T : EventContent
Default:
n/a
Reified type parameter bounded by the EventContent interface, allowing access to type information at runtime.
channel *
Type: String
Default:
n/a
Channel 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.
customMethod
Type: String
Default:
n/a
An optional custom method for emitting events. If not provided, defaults to null.
callback *
Type: noinline (event: Event<T>) -> Unit
Default:
n/a
A 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
AutoCloseable
Interface that lets you stop receiving invitation-related updates (invite events) by invoking the close() method.

Sample code

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

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