Android
Authentication

Authentication

In order to use the features of the Android SDK in your client apps, a SendBird instance must be initiated in each client app through user authentication with SendBird server. The instance communicates and interacts with the server using an authenticated user account, and is allowed to use the SDK's features. This page explains how to authenticate your user with the server.


Initialize the SDK with APP_ID

To use our chat features, you must initialize a SendBird instance by passing the APP_ID of your SendBird application to the SendBird.init() method as a parameter. The SendBird.init() must be called once in the onCreate() method of Application instance of your client app.

Light Color Skin
Copy
SendBird.init(APP_ID, context);

Connect to SendBird server with a user ID

By default, SendBird server can authenticate a user just by a unique user ID. Then the server queries the database to check for a match upon the request for connection. If no matching user ID is found, the server creates a new user account with the user ID. The ID must be unique within a SendBird application to be distinguished from others, such as a hashed email address or phone number in your service.

This authentication procedure is useful when in development or if your service doesn't require additional security.

Note: Go to the Event Handler page to learn more about the usages of Android SDK's handlers and callbacks.

Light Color Skin
Copy
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 server before calling any methods through the SDK (apart from init()). If you attempt to call a method without connecting, an ERR_CONNECTION_REQUIRED (800101) error would return.


Connect to SendBird server with a user ID and an access token

Using the Platform API, you can create a user along with their own access token, or issue an access token for an existing user. Once an access token is issued, a user is required to provide the access token in the SendBird.connect() method which is used for logging in.

  1. Using the API, create a SendBird user account with the information submitted when a user signs up or in to your service.
  2. Save the user ID along with the issued access token to your persistent storage which is securely managed.
  3. When the user attempts to log in to the SendBird application, load the user ID and access token from the storage, and then pass them to the SendBird.connect() method.
  4. Periodically replacing the user's access token is recommended to protect the account.

Note: From Settings > Application > Security > Access token permission setting in your dashboard, you are able to prevent users without an access token from logging in to your SendBird application or restrict their access to read and write messages.

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

Note: For security reasons, you can also use a session token when a user logs in to SendBird server instead of an access token. Go to the Access token vs. Session token section from the Platform API guide to learn more.


Disconnect from SendBird server

A user should be disconnected from SendBird server when they no longer need to receive messages from an online state. However, the user will still receive push notifications for new messages from group channels they've joined.

When disconnected, all event handlers in a user's client app registered by the SendBird.addChannelHandler() or SendBird.addConnectionHandler() stop receiving event callbacks from the server. Then, all internally cached data in the client app, such as the channels that are cached when the OpenChannel.getChannel() or GroupChannel.getChannel() is called, are also flushed.

Note: By default, most of the data related to users, channels, and messages are internally cached in the SendBird instance of a user's client app, which are retrieved by the corresponding query instances or received through the event handlers.

Light Color Skin
Copy
SendBird.disconnect(new SendBird.DisconnectHandler() {
    @Override
    public void onDisconnected() {
        // A current user is disconnected from SendBird server.
    }
});

Update user profile

Using the updateCurrentUserInfo() method, you can update a user's nickname and profile image with a URL.

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

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

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

Note: A user's profile image can be a JPG (.jpg), JPEG (.jpeg), or PNG (.png) file of up to 25 MB.