MagicBell Android SDK

The MagicBell Android SDK allows you to integrate MagicBell into your Android app. It offers real-time notifications, low-level wrappers for the MagicBell API, and support for the Compose framework.


Add the dependency in your build.gradle file.

// MagicBell SDK
implementation 'com.magicbell:magicbell-sdk:1.0.0'
// MagicBell Compose
implementation 'com.magicbell:magicbell-sdk-compose:1.0.0'

Quick Start

import com.magicbell.sdk.MagicBellClient

// Create the MagicBell client with your project's API key
val magicbell = MagicBellClient(
  apiKey = "[MAGICBELL_API_KEY]",
  context = applicationContext

// Set the MagicBell user
val user = magicbell.connectUserEmail("")

// Create a store of notifications
val store =

// Fetch the first page of notifications. There is also a method without coroutine.
coroutineScope.launch {
    onSuccess = { notificationList ->

    onFailure = { error ->


The MagicBell Client

The first step is to create a MagicBellClient instance. It will manage users and other functionality for you. The API key for your MagicBell project is required to initialize it.

val magicbell = MagicBellClient(
  apiKey = "[MAGICBELL_API_KEY]",
  context = applicationContext

You can provide additional options when initializing a client:

val magicbell = MagicBellClient(
  apiKey = "[MAGICBELL_API_KEY]",
  apiSecret = "[MAGICBELL_SECRET_KEY]",
  enableHMAC = false,
  baseURL = defaultBaseUrl,
  logLevel = LogLevel.NONE,
  context = applicationContext,
  magicBellScope = coroutineScope
ParamDefault ValueDescription
apiKey-Your MagicBell's API key
apiSecretnilYour MagicBell's API secret
enableHMACfalseSet it to true if you want HMAC enabled. Note the apiSecret is required if set to true
logLevel.noneSet it to .debug to enable logs
context-Application Context
logLevelDispatchers(Main)Scope to run all the tasks.

Though the API key is meant to be published, you should not distribute the API secret. Rather, enable HMAC in your project and generate the user secret on your backend before distributing your app.

Integrating into your app

You should create the client instance as early as possible in your application and ensure that only one instance is used across your application.

import com.magicbell.sdk.MagicBellClient

// Store the instance at a place of your convenience
val magicbell = MagicBellClient("[MAGICBELL_API_KEY]")

We recommend to create the instance in your Application class or in your Dependency Injection graph as a Singleton.


Requests to the MagicBell API require that you identify the MagicBell user. This can be done by calling the connectUser(...) method on the MagicBellClient instance with the user's email or external ID:

// Identify the user by its email
val user = magicbell.connectUserEmail("")

// Identify the user by its external id
val user = magicbell.connectUserExternalId("001")

// Identify the user by both, email and external id
val user = magicbell.connectUserWith(email = "", externalId = "0001")

You can connect as many users as you need.

IMPORTANT: User instances are singletons. Therefore, calls to the connectUser method with the same arguments will yield the same user:

val userOne = magicbell.connectUserEmail("")
val userTwo = magicbell.connectUserEmail("")

assert(userOne === userTwo, "Both users reference to the same instance")

Multi-User Support

If your app supports multiple logins, you may want to display the status of notifications for all logged in users at the same time. The MagicBell SDK allows you to that.

You can call the connectUser(:) method with the r external ID of your logged in users as many times as you need.

val userOne = magicbell.connectUserEmail("")
val userTwo = magicbell.connectUserEmail("")
val userThree = magicbell.connectUserExternalId("001")

Logout a User

When the user is logged out from your application you want to:

  • Remove user's notifications from memory
  • Stop the real-time connection with the MagicBell API
  • Unregister the device from push notifications

This can be achieved with the disconnectUser method of the MagicBell client instance:

// Remove by email

// Remove by external id

// Remove by email and external id
magicbell.disconnectUserWith(email = "", externalId = "001")

Integrating into your app

The MagicBell User instances need to available across your app. Here you have some options:

  • extend your own user object
  • define a global attribute
  • use your own dependency injection graph

Extend your own user object

This approach is useful if you have a user object across your app. MagicBell will guarantee the User instance for a given email/externalId is unique, and you only need to provide access to the instance. For example:


// Your own user
data class User {
  val name: String
  val email: String

/// Returns the logged in MagicBell user
fun User.magicBell(): MagicBell.User {
  return magicbell.connectUserEmail(email)

Define a global attribute

This is how you can define a nullable global variable that will represent your MagicBell user:

val magicbell = MagicBellClient("[MAGICBELL_API_KEY]")
var magicbellUser: MagicBell.User? = null

As soon as you perform a login, assign a value to this variable. Keep in mind, you will have to check the magicbellUser variable was actually set before accessing it in your code.

Use your own dependency injection graph

You can also inject the MagicBell User instance in your own graph and keep track on it using your preferred pattern.


Obtaining a notification store

The NotificationStore class represents a collection of MagicBell notifications. You can create an instance of this class through the .build(...) method on the user store object.

For example:

val allNotifications =

val readNotifications = = true)

val unreadNotifications = = false)

val archivedNotifications = = true)

val billingNotifications = = ["billing"])

val firstOrderNotifications = = ["order:001"])

These are the attributes of a notification store:

totalCountIntThe total number of notifications
unreadCountIntThe number of unread notifications
unseenCountIntThe number of unseen notifications
hasNextPageBoolWhether there are more items or not when paginating forwards
countIntThe current number of notifications in the store
predicateStorePredicateThe predicate used to filter notifications

And these are the available methods:

refreshResets the store and fetches the first page of notifications
fetchFetches the next page of notifications
get(index:)Subscript to access the notifications: store[index]
deleteDeletes a notification
deleteDeletes a notification
markAsReadMarks a notification as read
markAsUnreadMarks a notification as unread
archiveArchives a notification
unarchiveUnarchives a notification
markAllReadMarks all notifications as read
markAllUnseenMarks all notifications as seen

Most methods have two implementations:

  • Using suspended functions returning a Result object
  • Using lambdas returning onSucess or onFailure
// Delete notification. Lambdas
  onCompletion = {
    println("Notification deleted")
  onFailure = {
    print("Failed: ${error})")

// Read a notification
  onSuccess = {
    println("Notification marked as read")
  onFailure = {
    println("Failed: $error")


These methods ensure the state of the store is consistent when a notification changes. For example, when a notification is read, stores with the predicate read: .unread, will remove that notification from themselves notifying all observers of the notification store.

Advanced filters

You can also create stores with more advanced filters. To do it, fetch a store using the .build(...) method with a StorePredicate.

val predicate = StorePredicate()
val notifications =

These are the available options:

readtrue, false, nilnilFilter by the read state (nil means unspecified)
seentrue, false, nilnilFilter by the seen state (nil means unspecified)
archivedtrue, falsefalseFilter by the archived state
categories[String][]Filter by categories
topics[String][]Filter by topics

For example, use this predicate to fetch unread notifications of the "important" category:

val predicate = StorePredicate(read = true, categories = ["important"])
val store =

Notification stores are singletons. Creating a store with the same predicate twice will yield the same instance.

Note: Once a store is fetched, it will be kept alive in memory so it can be updated in real-time. You can force the removal of a store using the .dispose method.

val predicate = StorePredicate()

This is automatically done for you when you remove a user instance.

Observing changes

When either fetch or refresh is called, the store will notify the content observers with the newly added notifications (read about observers here).

// Obtaining a new notification store (first time)
val store =

// First loading
val listNotifications = store.fetch().getOrElse {
  // An error occurred

To reset and fetch the store:

    val listNotifications = store.refresh().getOrElse {
  // An error occurred

Accessing notifications

The NotificationStore is a list and has all list methods available. Therefore, notifications can be accessed as expected:

// forEach
store.forEach { notification ->
  println("Notification = $notification")

// for in
for (notification in store) {
  println("Notification = $notification")

// As an array
val notifications = store.notifications()

Enumeration is also available:

// forEach
store.forEachIndexed { index, notification ->
  println("Notification = $notification is in position $index")

Observing notification store changes

Kotlin flow

NotificationStore exposes two flows with Content changes and Count changes. You can subscribe both of them to receive all the changes in the store. For Content event returns:

sealed class NotificationStoreContentEvent {
  object Reloaded : NotificationStoreContentEvent()
  class Inserted(val indexes: List) : NotificationStoreContentEvent()
  class Changed(val indexes: List) : NotificationStoreContentEvent()
  class Deleted(val indexes: List) : NotificationStoreContentEvent()
  class HasNextPageChanged(val hasNextPage: Boolean) : NotificationStoreContentEvent()

For Count events returns:

sealed class NotificationStoreCountEvent {
  class TotalCountChanged(val count: Int) : NotificationStoreCountEvent()
  class UnreadCountChanged(val count: Int) : NotificationStoreCountEvent()
  class UnseenCountChanged(val count: Int) : NotificationStoreCountEvent()

Example. Subscribe to the flows:

yourScope.launch {
  store.contentFlow.onEach { contentEvent ->
    // when(contentEvent)
    println("Content $it)

  store.countFlow.onEach { countEvent ->
    // when(countEvent)
    print("Count $it")

Classic Observer Approach

Instances of NotificationStore are automatically updated when new notifications arrive, or a notification's state changes (marked read, archived, etc.)

To observe changes on a notification store, your observers must implement the following protocols:

// Get notified when the list of notifications of a notification store changes
interface NotificationStoreContentObserver {
  fun onStoreReloaded()
  fun onNotificationsChanged(indexes: List)
  fun onNotificationsDeleted(indexes: List)
  fun onStoreHasNextPageChanged(hasNextPage: Boolean)

// Get notified when the counters of a notification store change
interface NotificationStoreCountObserver {
  fun onTotalCountChanged(count: Int)
  fun onUnreadCountChanged(count: Int)
  fun onUnseenCountChanged(count: Int)

To observe changes, implement these protocols (or one of them), and register as an observer to a notification store.

val store =
val observer = myObserverClassInstance


Compose Approach

Use the class NotificationStoreViewModel to create a reactive object compatible with Compose and capable of publishing changes on the main attributes of a NotificaitonStore.

This object must be created and retained by the user whenever it is needed.

totalCountState IntThe total count
unreadCountState IntThe unread count
unseenCountState IntThe unseen count
hasNextPageState BoolBool indicating if there is more content to fetch.
notificationsState [Notification]The array of notifications.

Notification Store adapter

The Notification Store is a list also and we recommend to use it in your RecyclerView adapters. Thanks to the observers you can refresh your notification list very easy and with animations.

class NotificationsAdapter(
  var store: NotificationStore,
  private val notificationClick: (Notification, Int) -> Unit,
) : RecyclerView.Adapter()

Another option would be to have your own list of notifications and modify it everytime that the user does an action.

User Preferences

You can fetch and set user preferences for MagicBell channels and categories.

class Preferences {
  var email: Bool
  var inApp: Bool
  var mobilePush: Bool
  var webPush: Bool

class UserPreferences {
  val preferences: Map

To fetch user preferences, use the fetch method as follows:

user.preferences.fetch().fold(onSuccess = { userPreferences ->
}, onFailure = {
  // An error occurred

It is also possible to fetch preference for a category using the fetchPreferences(for:) method:

user.preferences.fetchPreferences("important").fold(onSuccess = { preferences ->
}, onFailure = {
  // An error occurred

To update the preferences, use either update or updatePreferences(:for:).

// Updating all preferences at once.
user.preferences.update().getOrElse { }

// Updating the list of preferences for a category
// Only preference for the included categories will be changed
user.preferences.update(categoryPreferences, "important").getOrElse { }

Push Notifications

You can register the device token with MagicBell for mobile push notifications. To do it, set the device token as soon as it is provided by FCM or your notification SDK:

// FCM Example
FirebaseMessaging.getInstance().token.addOnCompleteListener(OnCompleteListener { task ->
  if (!task.isSuccessful) {
    Log.w(TAG, "Fetching FCM registration token failed", task.exception)

  // Get new FCM registration token
  val token = task.result

  // Log and toast

MagicBell will keep that device token stored temporarily in memory and send it as soon as new users are declared via MagicBellClient.connectUser.

Whe a user is disconnected (MagicBellClient.disconnectUser), the device token is automatically unregistered for that user.


We welcome contributions of any kind. To do so, clone the repo and open build.gradle with Android Studio Arctic Fox or above.