Authentication

To use SendBird's features, first do the following authentication tasks.


Initializing with APP_ID

To use the chat features, you must initialize SendBird using the APP_ID assigned to your SendBird application. Typically, initialization is implemented in the user login screen.

SendBirdClient.Init(APP_ID);

Connecting with UserID

By default, SendBird requires only a UserID to join a channel. Upon requesting connection, SendBird queries its user database for a matching UserID. If it finds that the UserID has not been registered yet, SendBird creates a new user account. The UserID can be any unique string id, such as an email address or a UID from your database.

This simple authentification procedure is useful when you are in development or if your service does not require additional security.

Note: Explanation on SendBird's usage of Handlers and callbacks can be found under the Event Handler section.

SendBirdClient.Connect(USER_ID, (User user, SendBirdException e) =>
{
    if(e != null)
    {
        // Error
        return;
    }
});

Connecting with UserID and Access Token

With the SendBird Platform API, you can create a user with an access token, or you can issue an access token for an existing user. Once an access token is issued, you are required to provide the user's token in the login method.

  1. Create a SendBird user account via the Platform API when your user signs up on your service.
  2. Save the access token to your secured persistent store.
  3. Load the access token in your client and pass it to the SendBird login method.
  4. For security reasons, we recommend that you periodically update your access token by issuing a new token to replace the previous one.

Note: You can set restrictions for users without access tokens in your Dashboard settings. These settings can be found under Security - Access Token Policy.

SendBirdClient.Connect(USER_ID, ACCESS_TOKEN, (User user, SendBirdException e) =>
{
    if(e != null)
    {
        // Error
        return;
    }
});

Disconnecting

You must disconnect from SendBird when your user no longer needs to receive messages from an online state. But users receive group channel messages through Push Notifications.

Disconnecting removes all registered handlers and callbacks. That is, it removes all Event Handlers added through SendBirdClient.AddChannelHandler() or SendBirdClient.AddConnectionHandler(). It also flushes all internally cached data, such as the channels that are cached when OpenChannel.GetChannel() or GroupChannel.GetChannel() is called.

SendBirdClient.Disconnect(() => {
    @Override
    public void onDisconnected() {
        // You are disconnected from SendBird.
    }
});

Updating a user profile and profile image

You can update a user's nickname and profile image. Call UpdateCurrentUserInfo() to update a user's nickname, as well as their profile picture with a URL.

SendBirdClient.Connect(USER_ID, (User user, SendBirdException e) =>
{
    if(e != null)
    {
        // Error
        return;
    }

    SendBirdClient.UpdateCurrentUserInfo(NICKNAME, PROFILE_URL, (SendBirdException e1) =>
    {
        if(e1 != null)
        {
            // Error
            return;
        }
    });
});

Or, you can upload an image directly using UpdateCurrentUserInfoWithProfileImage().

SendBirdClient.Connect(USER_ID, (User user, SendBirdException e) =>
{
    if(e != null)
    {
        // Error
        return;
    }

    SendBirdClient.UpdateCurrentUserInfoWithProfileImage(NICKNAME, FILE_PATH, (SendBirdException e1) =>
    {
        if(e1 != null)
        {
            // Error
            return;
        }
    });
});