PubNub Logo Docs
Support Contact Sales Login Try Our APIs

›ANDROID

Collapse all
Dark mode

Back to Home

Overview

  • In-App Chat

Chat Components

  • Overview
  • REACT

    • React Components

    ANDROID

    • Getting Started
    • UI Components
    • Data Components
    • Chat Provider

    IOS

    • Getting Started
    • UI Components
    • Data Components
    • Chat Provider

SDKs

  • Overview
  • USERS

    • Setup
    • Metadata
    • Permissions
    • Presence
    • Mentions

    CHANNELS

    • Types and Names
    • Metadata
    • Subscriptions
    • Memberships

    MESSAGES

    • Sending Messages
    • Message Storage
    • Unread Counts
    • File Upload
    • Typing Indicators
    • Read Receipts
    • Emoji Reactions
    • Update Messages
    • Delete Messages
    • Message Webhooks

    PUSH NOTIFICATIONS

    • Overview

    MODERATION

    • Profanity Filters
    • Flag Messages
    • Ban Users
    • Mute Users
    • Spam Prevention

    INTEGRATIONS

    • Overview
    • Content Moderation
    • Image Moderation
    • Language Translation
    • Chatbots
    • GIFs
    • Stickers

Moderation Dashboard

  • Overview
  • Getting Started
  • FEATURES

    • Automatic Text Moderation
    • Automatic Image Moderation
    • Manual Message Moderation
    • Manual User Moderation
    • User Management
    • Channel Management
  • Required Configuration

Debug Console
Network Status

Chat Provider

ChatProvider is the general interface for UI and data operations across the component library. It provides default implementations of themes, repositories, services, and utils for the composition tree through CompositionLocalProvider.

You can easily configure ChatProvider to synchronize data from different services, including messages and channel occupancy, by setting its synchronize parameter to true.

Optimized data flow

Jetpack Compose passes data through the composition tree explicitly by means of parameters for composable functions. Using a tree is most often the simplest way to implement a data flow between services in your application.

Configuration

You can configure ChatProvider using the following parameters.

Required parameters

Required parameters don't have default values. If a parameter has a default value, it's optional.

ParameterTypeDefault valueDescription
pubNubPubNubn/aPubNub instance
databasePubNubDatabase<MessageDao<DBMessage, DBMessage>, ChannelDao<DBChannel, DBChannelWithMembers>, MemberDao<DBMember, DBMemberWithChannels>, MembershipDao<DBMembership>>Database.INSTANCEDatabase object. It's used by all the repositories for CRUD operations (Create, Read, Update, Delete).
channelChannelId"channel.lobby"ID of the channel to subscribe to.
synchronizeBooleantrueParameter for data synchronization. If set to true, it will synchronize data in MessageService and OccupancyService.
content@Composable() () -> Unitn/aComposable content to draw.

Persistent database

For the repositories to work, ChatProvider creates the SQLite database. Additionally, it uses the Room library to provide an abstraction layer over SQLite and allow more robust data access.

To learn more about Room, refer to the official Android docs.

Structure

To be able to use repositories, ChatProvider creates a default database structure. The database object must implement the PubNubDatabase interface. This interface allows repositories to operate on the predefined data access objects (DAOs).

The following DAOs are declared:

  • MessageDao contains all the chat messages.
  • ChannelDao contains all the channels.
  • MemberDao contains user objects.
  • MembershipDao contains the relation between members and channels.
interface PubNubDatabase<Message : MessageDao<*, *>, Channel : ChannelDao<*, *>, Member : MemberDao<*, *>, Membership : MembershipDao<*>> {
    fun messageDao(): Message
    fun channelDao(): Channel
    fun memberDao(): Member
    fun membershipDao(): Membership
}

Default initialization

To initialize the database with the default structure, you can use the Database.initialize(context) helper method. You must call this method with the application context as a parameter.

ParameterTypeDefault valueDescription
applicationContextContextn/aApplication context needed to create a database.
builder(RoomDatabase.Builder<DefaultDatabase>) -> RoomDatabase.Builder<DefaultDatabase>{ it }Room database builder. You can use it to add callbacks or initialize data.
fun initialize(
  applicationContext: Context,
  builder: (RoomDatabase.Builder<DefaultDatabase>) -> RoomDatabase.Builder<DefaultDatabase> = { it }
)

Basic example

Call initialize() from the onCreate() method in Application().

class ChatComponentsApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        Database.initialize(this)
    }
}

Callback example

To use the Room callback, you must pass it as a parameter. In the example, the message is logged after creating and opening the database.

class ChatComponentsApplication : Application() {

    override fun onCreate() {
        super.onCreate()
        Database.initialize(applicationContext) { database ->
            database.addCallback(
                object : RoomDatabase.Callback() {
                    override fun onOpen(db: SupportSQLiteDatabase) {
                        super.onOpen(db)
                        Log.d("Database", "onOpen called")
                    }

                    override fun onCreate(db: SupportSQLiteDatabase) {
                        super.onCreate(db)
                        Log.d("Database", "onCreate called")
                    }
                }
            )
        }
    }
}

Local providers

Providers created in ChatProvider can be grouped by one of these types:

  • Common
  • Utils
  • Themes
  • Repositories
  • Services

Common

Providers responsible for storing unspecified data:

  • LocalPubNub holds a PubNub instance.
  • LocalChannel holds the current chat channel.
  • LocalUser holds the current user object.

Utils

Providers responsible for formatting and error handling:

  • LocalMemberFormatter transforms a user ID into a member object.
  • LocalErrorHandler can be used for error handling across all the components.

Themes

List of providers used for styling components:

  • LocalMessageInputTheme describes the style for the MessageInput component.
  • LocalTypingIndicatorTheme describes the style for the TypingIndicator component.
  • LocalChannelListTheme describes the style for the ChannelList component.
  • LocalMemberListTheme describes the style for the MemberList component.
  • LocalMessageListTheme describes the style for the MessageList component.
  • LocalMessageTheme describes the style for the Message object.
  • LocalIndicatorTheme describes the style for the PresenceIndicator state.
  • LocalProfileImageTheme describes the style for the ProfileImage component.

Repositories

List of repository providers:

  • LocalChannelRepository provides an instance of ChannelRepository.
  • LocalMessageRepository provides an instance of MessageRepository.
  • LocalMemberRepository provides an instance of MemberRepository.
  • LocalMembershipRepository provides an instance of MembershipRepository.

Services

List of service providers:

  • LocalMessageService provides the MessageService implementation.
  • LocalChannelService provides the ChannelService implementation.
  • LocalMemberService provides the MemberService implementation.
  • LocalTypingService provides the TypingService implementation.
  • LocalOccupancyService provides the OccupancyService implementation.
←Data ComponentsGetting Started→
  • Configuration
  • Persistent database
    • Structure
    • Default initialization
  • Local providers
    • Common
    • Utils
    • Themes
    • Repositories
    • Services
© PubNub Inc. - Privacy Policy