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.

Unity 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)

Input

ParameterTypeRequiredDefaultDescription
userUserYesn/aUser that you want to invite to a 1:1 channel.

Output

TypeDescription
MembershipReturned (modified) object containing the membership data.

Basic usage

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

// reference "support-agent-15"
if (!chat.TryGetUser("support-agent-15", out var user))
{
Console.WriteLine("Couldn't find user to ivite!");
return;
};

// get the channel
if (!chat.TryGetChannel("high-prio-incidents", out var channel))
{
Console.WriteLine("Couldn't find channel to invite!");
return;
};

// invite the agent to join the channel
show all 16 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(List<User> users)

Input

ParameterTypeRequiredDefaultDescription
usersList<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
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.

// reference "support-agent-15"
if (!chat.TryGetUser("support-agent-15", out var user1))
{
Console.WriteLine("Couldn't find first user!");
return;
};

// reference "support-agent-16"
if (!chat.TryGetUser("support-agent-16", out var user2))
{
Console.WriteLine("Couldn't find second user!");
return;
};

// reference the "high-prio-incidents" channel
show all 23 lines

Listen to Invite events

As an admin of your chat app, you can use the StartListeningForInviteEvents() 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.

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:

// start listening
chat.StartListeningForInviteEvents(string userId)

// triggered invite event
public event Action<ChatEvent> OnInviteEvent;
// needs a corresponding event handler
void EventHandler(InviteEvent event)
Input
ParameterTypeRequiredDefaultDescription
userIdstringYesn/aThe invited user.
Output

This method doesn't return any data.

Basic usage

Print a notification when the user support-agent-2 is invited to the support channel.

//start listening
chat.StartListeningForInviteEvents(string userId)
//lambda event handler
chat.OnInviteEvent += (inviteEvent) =>
{
if(inviteEvent.ChannelId == "support" && inviteEvent.UserId == "support-agent-2")
{
Console.WriteLine("User support-agent-2 has been invited to the support channel!");
}
};
Last updated on