Version 1
Home
/
Notifications
/
UIKit
Notifications v1
Notifications

Install Chat UIKit for Notifications

Copy link

Sendbird Chat UIKit for Notifications is a set of prebuilt UI components that allows you to easily craft an in-app notification channel. Our development kit includes light and dark themes, fonts, colors and more. You can install Chat UIKit for Notifications on the client app to receive notifications sent through the Notifications API or an external platform. Follow the steps below.

Note: Sendbird Notifications uses the existing Chat UIKit for Android v3.5.0 and later. To learn more about Sendbird UIKit for Chat, refer to this page.


Requirements

Copy link

The minimum requirements for Chat UIKit Android for Notifications are:

  • Android 5.0 (API level 21) or higher
  • Java 8 or higher
  • Support androidx only
  • Android Gradle plugin 4.0.1 or higher
  • Sendbird Chat SDK for Android 4.6.0 and later
  • Sendbird Chat UIKit for Android 3.5.0 and later

Before you start

Copy link

Before installing Sendbird Chat SDK, you need to create a Sendbird application on Sendbird Dashboard, which comprises everything required in a chat and notification service including users, notifications, and channels. You will need the Application ID of your Sendbird application when initializing the SDK.

Note: Each Sendbird application can only be integrated with a single client app.


Get started

Copy link

You can start building a notification channel in your app by installing Chat UIKit for Notifications. This developer kit is an add-on feature to Sendbird Chat SDK so installing it will also install the core Chat SDK.

Step 1 Create a project

Copy link

To get started, open Android Studio and create a new project for the UIKit in the Project window as follows:

  1. Click Start a new Android Studio project in the Welcome to Android Studio window.
  2. Select Empty Activity in the Select a Project Template window and click Next.
  3. Enter your project name in the Name field in the Configure your project window.
  4. Select your language as either Java or Kotlin from the Language drop-down menu.
  5. Make sure Use legacy android.support.libraries is unchecked.
  6. Select minimum API level as 21 or higher.

Step 2 Install Chat UIKit for Notifications

Copy link

You can install Chat UIKit for Android through Gradle. If using Gradle 6.7 or lower, add the following code to your root build.gradle file:

allprojects {
    repositories {
        maven { url "https://jitpack.io" }
        maven { url "https://repo.sendbird.com/public/maven" }
    }
}

Note: Make sure the above code block isn't added to your module build.gradle file.

If using Gradle 6.8 or higher, add the following to your settings.gradle file:

dependencyResolutionManagement {
    repositories {
        maven { url "https://jitpack.io" }
        maven { url "https://repo.sendbird.com/public/maven" }
    }
}

Note: To learn more about updates to Gradle, see Gradle's release note.

Next, for all Gradle versions, open the build.gradle file at the application level. For both Java and Kotlin, add the following code block and dependencies:

apply plugin: 'com.android.application'

android {
    buildFeatures {
        viewBinding true
    }

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8    // Make sure you have JavaVersion 1.8.
        targetCompatibility JavaVersion.VERSION_1_8    // Make sure you have JavaVersion 1.8.
    }
}

dependencies {
    implementation 'com.sendbird.sdk:uikit:3.+'
}

Before saving the build.gradle file, check if you’ve enabled viewBinding. Then, click the Sync button to apply all changes.

Note: UIKit SDK versions 2.1.1 or lower can be downloaded from JCenter until February 1, 2022. SDK versions higher than 2.1.1 will be available on Sendbird's remote repository.

Step 3 Initialize SendbirdUIKit instance

Copy link

To integrate and run Chat UIKit for Notifications in your app, you need to initialize it first. You can initialize SendbirdUIKit instance by passing the SendbirdUIKitAdapter instance as an argument to a parameter in the SendbirdUIKit.init() method. The SendbirdUIKit.init() must be called once in the onCreate() method of your app’s Application instance.

Note: The UIKit uses local caching so that the client app can locally cache and retrieve channel and message data. The initialization process of the SendbirdUIKit instance is asynchronous and requires you to receive a callback function before you can move onto the next step. If the database fails to migrate, the onInitFailed() method is called. If the database successfully migrates, the onInitSucceed() method is called and you can proceed to the next step. Refer to the updated code below.

JavaKotlin
import android.app.Application;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.sendbird.uikit.SendbirdUIKit;
import com.sendbird.uikit.adapter.SendbirdUIKitAdapter;
import com.sendbird.uikit.interfaces.UserInfo;
import com.sendbird.android.handler.InitResultHandler;
import com.sendbird.android.exception.SendbirdException;

public class BaseApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();

        SendbirdUIKit.init(new SendbirdUIKitAdapter() {
            @NonNull
            @Override
            public String getAppId() {
                return "YOUR_APP_ID";  // Specify your Sendbird application ID.
            }

            @Nullable
            @Override
            public String getAccessToken() {
                return "";
            }

            @NonNull
            @Override
            public UserInfo getUserInfo() {
                return new UserInfo() {
                    @Override
                    public String getUserId() {
                        return "USER_ID";  // Specify your user ID.
                    }

                    @Nullable
                    @Override
                    public String getNickname() {
                        return "USER_NICKNAME";  // Specify your user nickname.
                    }

                    @Nullable
                    @Override
                    public String getProfileUrl() {
                        return "";
                    }
                };
            }

            @NonNull
            @Override
            public InitResultHandler getInitResultHandler() {
                return new InitResultHandler() {
                    @Override
                    public void onMigrationStarted() {
                        // DB migration has started.
                    }

                    @Override
                    public void onInitFailed(SendbirdException e) {
                        // If DB migration fails, this method is called.
                    }

                    @Override
                    public void onInitSucceed() {
                        // If DB migration is successful, this method is called and you can proceed to the next step.
                        // In the sample app, the LiveData class notifies you on the initialization progress
                        // And observes the MutableLiveData<InitState> initState value in SplashActivity().
                        // If successful, the LoginActivity screen
                        // Or the HomeActivity screen will show.
                    }
                };
            }
        }, this);
    }
}

Step 4 Connect to Sendbird server

Copy link

Connect a user to the Sendbird server using the SendbirdUIKit.connect() method with the information you provided in SendbirdUIKitAdapter. The connect() method also automatically updates the user profile on the server.

With local caching added to Sendbird Chat SDK, the latest user instance may be returned through the callback even when the user is offline. The local caching functionality stores message and channel data in the local storage and Sendbird server. As a result, even when a user is not connected to the server, the user information stored in the local cache is returned through the callback along with an error indicating the offline status.

Refer to the code below to see how to connect a user to the Sendbird server:

JavaKotlin
SendbirdUIKit.connect(new ConnectHandler() {
   @Override
   public void onConnected(User user, SendbirdException e) {
       if (user != null) {
           if (e != null) {
               // Proceed in offline mode with the data stored in the local database.
               // Then when a connection is made automatically,
               // you can be notified through ConnectionHandler.onReconnectSucceeded().
           } else {
               // Proceed in online mode.
           }
       } else {
           // Handle error.
       }
   }
});

Note: Chat UIKit for Notifications automatically establishes a connection when necessary. If a connection with the Sendbird server is required before using a UIKit component, use the SendbirdUIKit.connect() method to establish a connection.

Step 5 Add BaseApplication

Copy link

Add the created BaseApplication to AndroidManifest.xml.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sendbird.uikitapplication">

    <!--  sets android:name  -->
    <application

        android:name=".BaseApplication"

        >
    </application>
</manifest>

Step 6 Register push notification credentials

Copy link

In order to receive push notifications, you need to register notification credentials first. You can set up push notifications for either FCM or HMS. For FCM, register your server key and a registration token. For HMS, register your app ID and app secret.

You can also implement push notifications with multi-device support if you want to send a push notification even when the client app is in the foreground. Refer to the multi-device support guide for FCM or HMS.

Step 7 Display notification channel

Copy link

Refer to the codes below to display either Feed view or channel list view where you can see the notifications that were sent to the channel.

Feed view

Copy link

In order to display the Feed view of your notification channel, you need to first find its channel URL on Sendbird Dashboard under Notifications > Channels.

Start an activity by using intent to move from one activity to FeedNotificationChannelActivity.

JavaKotlin
Intent intent = FeedNotificationChannelActivity.newIntent(context, "FEED_CHANNEL_CHANNEL_URL");
startActivity(intent);

FeedNotificationChannelActivity allows you to create a basic FeedNotificationChannelFragment through UIKitFragmentFactory and FeedNotificationChannelFragment.Builder. UIKitFragmentFactory has a set of methods that build each fragment, whereas the builder class provides APIs to customize the UI of the data and event handlers used in FeedNotificationChannelFragment.

JavaKotlin
FeedNotificationChannelFragment fragment = new FeedNotificationChannelFragment.Builder("FEED_CHANNEL_CHANNEL_URL").build();

Channel list view

Copy link

To see the Chat view of your notification channel, you need to first display and call the channel list view in the UIKit. The channel list contains a list of all group channels that the user is a member of along with the notification channel.

Start an activity by using intent to move from one activity to ChannelListActivity.

JavaKotlin
Intent intent = ChannelListActivity.newIntent(context);
startActivity(intent);

ChannelListActivity allows you to create a basic ChannelListFragment through UIKitFragmentFactory and ChannelListFragment.Builder. UIKitFragmentFactory has a set of methods that build each fragment, whereas the builder class provides APIs to customize the UI of the data and event handlers used in ChannelListFragment.

JavaKotlin
ChannelListFragment fragment = new ChannelListFragment.Builder().build();