Android Introduction

The Android SDK enables you to build any type of chat or messaging experience for Android. It consists of three major components:

  • Client : The client handles all API calls and receives events.

  • Offline : The offline library stores the data, implements optimistic UI updates, handles network failures, and exposes StateFlow objects that make it easy to build your own UI on.

  • UI : The UI package includes ViewModels and custom views for common things like a channel list, message list, message input etc.

We typically recommend building a prototype with the full UI package. This is the fastest way to ensure that you have the integration set up correctly in combination with the backend and other teams. After that, about half of our customers launch in production with our UI package.

There are some limitations to Android UI reusability, so if you have more complicated design needs, you’ll want to drop down a level to the offline package. For most applications, you won’t have to use the client directly (note that it’s a lot of work to keep the chat state updated manually, which you’d have to do when using the client).

Before going through these docs, we recommend trying out the Android Chat tutorial and taking a look at the Android Chat Sample App. You can also check out our API docs for more information, as well our source code on GitHub.

The online API tour is also a nice way to learn about how the API works. It’s in-browser, and therefore Javascript based, but the ideas are pretty much the same in Kotlin.

Getting Started

This guide quickly brings you up to speed on Stream’s Chat API. The API is flexible and allows you to build any type of chat or messaging. Note that if you build on our UI components, you won’t need to call into this API too often after the initialization of the library.

Add one of the three packages below to your dependencies for your module/app level build.gradle file:

repositories {
  google()
  mavenCentral()
  maven { url "https://jitpack.io" }
  jcenter()
}

dependencies {
  // Client + offline + UI components
  implementation "io.getstream:stream-chat-android-ui-components:$stream_version"
  // Client + offline
  implementation "io.getstream:stream-chat-android-offline:$stream_version"
  // Client only
  implementation "io.getstream:stream-chat-android-client:$stream_version"
}

For the latest version, check our GitHub releases page.

Chat Client

Let’s get started by initializing the client and connecting the current user:

val apiKey = "{{ api_key }}"
val token = "{{ chat_user_token }}"
// Step 1 - Set up the OfflinePlugin for offline storage
val offlinePluginFactory = StreamOfflinePluginFactory(
  config = Config(
    backgroundSyncEnabled = true,
    userPresence = true,
    persistenceEnabled = true,
    uploadAttachmentsNetworkType = UploadAttachmentsNetworkType.NOT_ROAMING,
  ),
  appContext = applicationContext,
)

// Step 2 - Set up the client, together with offline plugin, for API calls
val client = ChatClient.Builder(apiKey, applicationContext)
  // Change log level
  .logLevel(ChatLogLevel.ALL)
  .withPlugin(offlinePluginFactory)
  .build()

// Step 3 - Authenticate and connect the user
val user = User(
  id = "summer-brook-2",
  name = "Paranoid Android",
  image = "https://bit.ly/2TIt8NR",
)

client.connectUser(
  user = user,
  token = token, // or client.devToken(userId); if auth is disabled for your app
).enqueue { result ->
  if (result.isSuccess) {
    // Handle success
  } else {
    // Handler error
  }
}

The user token is typically provided by your backend when you login or register in the app. If authentication is disabled for your app, you can also use a ChatClient#devToken to generate an insecure token for development. Of course, you should never launch into production with authentication disabled.

For more complex token generation and expiration examples, have a look at Token Expiration.

Channels

Let’s continue by creating your first channel. A channel contains messages, a list of members who are permanently associated with the channel, and a list of watchers currently watching the channel. The example below shows how to set up a channel:

val channelClient = client.channel(channelType = "messaging", channelId = "travel")

val extraData = mutableMapOf<String, Any>(
  "name" to "Awesome channel about traveling"
)

// Creating a channel with the low level client
channelClient.create(memberIds = emptyList(), extraData = extraData).enqueue { result ->
  if (result.isSuccess) {
    val channel: Channel = result.data()
    // Use channel by calling methods on channelClient
  } else {
    // Handle result.error()
  }
}

// Watching a channel's state using the offline library
scope.launch {
  client.watchChannelAsState(cid = "messaging:travel", messageLimit = 0).collect { channelState ->
    if (channelState != null) {
      // StateFlow objects to observe
      channelState.messages
      channelState.reads
      channelState.typing
    } else {
      // User not connected yet.
    }
  }
}

The first two arguments are the channel type and the channel ID ( messaging and travel in this case). In this case, the channel ID is explicit, but channels can also be created by specifying a list of members, in that case, an ID will be generated automatically.

The channel type controls the settings we’re using for this channel. There are four default types of channels:

  • livestream
  • messaging
  • team
  • commerce

These options provide you with the most sensible defaults for the given use cases. You can also define custom channel types if the defaults don’t work for your use case. See Channel Types for more info.

The extraData argument is an object containing custom channel data. You can add as many custom fields as you would like, as long as the total size of the object is less than 5KB. This applies not only to channels, but also messages, users, attachments, and reactions.

Messages

Now that we have the channel set up, let’s send our first chat message:

val message = Message(
  text = "I’m mowing the air Rand, I’m mowing the air.",
  cid = "messaging:travel",
  extraData = mutableMapOf("customField" to "123")
)

channelClient.sendMessage(message).enqueue { result ->
  if (result.isSuccess) {
    val message: Message = result.data()
  } else {
    // Handle result.error()
  }

When you send a message to a channel, Stream Chat automatically broadcasts to all the people that are watching this channel and updates in real-time.

Querying Channels

The client.queryChannels method enables you to retrieve a list of channels. You can specify a filter and sort order. The offline support library keeps the channels list updated as new messages, reactions and new channels arrive.

val filter = Filters.and(
  Filters.eq("type", "messaging"),
  Filters.`in`("members", "john"),
)
val sort = QuerySortByField<Channel>().descByName("lastMessageAt")

val request = QueryChannelsRequest(
  filter = filter,
  offset = 0,
  limit = 10,
  querySort = sort
).withWatch().withState()

client.queryChannels(request).enqueue { result ->
  if (result.isSuccess) {
    val channels: List<Channel> = result.data()
  } else {
    // Handle result.error()
  }
}

To learn more about which fields you can query and sort on have a look at the query channels documentation.

Conclusion

Now that you understand the building blocks of a fully functional chat integration, let’s move on to the next sections of the documentation, where we dive deeper into details on each API endpoint.

© Getstream.io, Inc. All Rights Reserved.