Authentication

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


Initialize the SDK with APP_ID

To use the chat features, you must initialize SendBird using the APP_ID assigned to your SendBird application. We strongly recommend that you call SendBird.init() once in the onCreate() method of Application class across the entire application.

SendBird.init(APP_ID, context);

Connect to SendBird server with UserID

By default, a SendBird server requires only a UserID to join a channel. Upon requesting connection, the SendBird server queries its user database for a matching UserID. If it finds that the UserID has not been registered yet, the SendBird server 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 authentication procedure is useful when you are in development or if your service does not require additional security.

Note: Explanation on the usages of Android SDK's handlers and callbacks can be found under the Event Handler section.

SendBird.connect(USER_ID, new ConnectHandler() {
    @Override
    public void onConnected(User user, SendBirdException e) {
        if (e != null) {    // Error.
            return;
        }
    }
});

Note: You must connect to SendBird before calling any methods through the SDK (apart from init()). If you attempt to call a method without connecting, you may receive an API-TOKEN is missing error.


Connect to SendBird server with UserID and Access Token

With the 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 SendBird.connect() 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.connect() method.
  4. For security reasons, we recommend updating your access token periodically 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.

SendBird.connect(USER_ID, ACCESS_TOKEN, new ConnectHandler() {
    @Override
    public void onConnected(User user, SendBirdException e) {
        if (e != null) {    // Error.
            return;
        }
    }
});

Disconnect from SendBird server

Disconnect from a SendBird server when your user no longer needs to receive messages from an online state. But users receive Group Channel messages through Push Notifications.

When disconnected from the server, all registered event handlers and callbacks are removed, which were added through SendBird.addChannelHandler() or SendBird.addConnectionHandler(). It also flushes all internally cached data, such as the channels that are cached when OpenChannel.getChannel() or GroupChannel.getChannel() is called.

SendBird.disconnect(new SendBird.DisconnectHandler() {
    @Override
    public void onDisconnected() {
        // You are disconnected from SendBird.
    }
});

Update user profile

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.

SendBird.connect(USER_ID, new ConnectHandler() {
    @Override
    public void onConnected(User user, SendBirdException e) {
        if (e != null) {    // Error.
            return;
        }

        SendBird.updateCurrentUserInfo(NICKNAME, PROFILE_URL, new UserInfoUpdateHandler() {
            @Override
            public void onUpdated(SendBirdException e) {
                if (e != null) {    // Error.
                    return;
                }
            }
        });
    }
});

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

SendBird.connect(USER_ID, new ConnectHandler() {
    @Override
    public void onConnected(User user, SendBirdException e) {
        if (e != null) {    // Error.
            return;
        }

        SendBird.updateCurrentUserInfoWithProfileImage(NICKNAME, FILE, new UserInfoUpdateHandler() {
            @Override
            public void onUpdated(SendBirdException e) {
                if (e != null) {    // Error.
                    return;
                }
            }
        });
    }
});