# Quickstart: Android

> Add the Herald SDK to an Android app, identify a user, track an event, register for push and show content cards.

This guide takes an Android app from no SDK to a verified first event, a registered push token and the default content card feed. It takes about 30 minutes.

:::prereq
- An app with `minSdk` 26 or higher that uses Jetpack Compose for the default views.
- A Firebase project with your Android app registered, and its `google-services.json`.
- A Herald sending connection with an app key. In Herald, open **Settings > Sending**, open the connection and find the **Mobile SDK** card. If it has no key, click **Generate app key**. The card also shows your endpoint URL.
- A backend route that returns a user token for the signed-in user, unless identity verification is **Off**. See [Users and identity](/docs/concepts/users-and-identity#user-tokens-and-verification-modes).
- For push: an FCM service account on the connection. See [Android push setup](/docs/push-notifications/android-setup).
:::

## 1. Add the modules

Add the JitPack repository, then `herald-core`, `herald-ui` and `herald-fcm`, and apply the Google services plugin so Firebase starts.

```kotlin
// settings.gradle.kts
dependencyResolutionManagement {
    repositories {
        google()
        mavenCentral()
        maven { url = uri("https://jitpack.io") }
    }
}

// app/build.gradle.kts
plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
    id("com.google.gms.google-services")
}

dependencies {
    implementation("com.github.ender1311.herald-android:herald-core:0.6.0")
    implementation("com.github.ender1311.herald-android:herald-ui:0.6.0")
    implementation("com.github.ender1311.herald-android:herald-fcm:0.6.0")
}
```

## 2. Configure the SDK in your Application class

Call `Herald.configure` once, in `Application.onCreate`. Calls made before `configure` are kept, up to 1,000, and applied in order once it runs.

```kotlin
class LumenApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        Herald.configure(
            this,
            appKey = "YOUR_APP_KEY",
            endpoint = "https://YOUR_HERALD_HOST",
            options = HeraldOptions(logLevel = LogLevel.DEBUG),
        )
        HeraldInAppPresenter.install()                  // default in-app message views
        HeraldNotifications.createDefaultChannel(this)  // channel for Herald pushes
        HeraldMessagingService.syncToken(this)          // registers the current FCM token
    }
}
```

Register the class in `AndroidManifest.xml` with `android:name=".LumenApplication"` on the `<application>` element. Every SDK call is thread-safe, returns at once and runs in order on one background thread.

## 3. Identify the user

After sign-in, call `identify` with your stable External ID, then `setUserToken` with the token your backend returns. Call them in that order: identifying a different user clears the previous token.

```kotlin
fun didSignIn(userId: String, userToken: String) {
    Herald.identify(userId, mapOf("plan" to "free"))
    Herald.setUserToken(userToken)
}
```

On sign-out, call `Herald.reset()`.

## 4. Track an event

```kotlin
Herald.track("workout_completed", mapOf("minutes" to 32, "type" to "strength"))
```

Events are queued on the device and sent in batches. [Events and attributes](/docs/concepts/events-and-attributes) lists the rules each call must follow.

## 5. Register for push

`herald-fcm` registers its own messaging service, so new FCM tokens reach Herald without more code. Ask for permission with `requestPushAuthorization`, which shows the Android 13 prompt when needed and records the answer, and pass notification taps to the SDK from your launcher activity.

```kotlin
class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Herald.handleNotificationIntent(intent)
        setContent { HomeScreen(onEnablePush = { Herald.requestPushAuthorization(this) }) }
    }

    override fun onNewIntent(intent: Intent) {
        super.onNewIntent(intent)
        Herald.handleNotificationIntent(intent)
    }

}
```

[Android push setup](/docs/push-notifications/android-setup) covers the service account, background notifications and apps that already have a messaging service.

## 6. Show content cards

`ContentCardFeed` from `herald-ui` shows the signed-in user's cards, pinned first and then newest, and logs impressions, clicks and dismissals for you.

```kotlin
@Composable
fun HomeScreen(onEnablePush: () -> Unit) {
    Column {
        Button(onClick = onEnablePush) { Text("Turn on workout reminders") }
        ContentCardFeed()
    }
}
```

## Verify it works

1. Run the app, sign in and log a workout. Send the app to the background: the SDK sends its queue as the app leaves the foreground.
2. In Logcat, filter by the tag `Herald`. With debug logging on, `Sent 3 events` (or similar) confirms a batch was accepted. `Herald rejected a batch` means it was not.
3. In Herald, open **Audience > Search users** and look up the External ID. The profile shows the traits you passed to `identify`, such as `plan`.
4. Have an agent send a content card to that user through the same connection. The card appears at the next session start, or right away if you call `Herald.refreshContentCards()`.

## Troubleshooting

- **One `Message sync needs a user token; waiting for setUserToken` debug line right after sign-in.** With verification **Required**, `identify` fetches messages before the token is set. It is harmless: `setUserToken` fetches again. Call `setUserToken` immediately after `identify`. A session-start in-app message can first show in the user's next session. A `Message sync got 401: Herald refused the user token` warning is different: the token the app sent is wrong. See [Error codes](/docs/reference/error-codes).
- **Nothing is sent at all.** `configure` never ran. Calls made before it are kept (up to 1,000) but only sent once it runs. Call `configure` at the top of `Application.onCreate` and check the manifest names your Application class.
- **`Herald rejected the app key: check the publishable key in configure`.** The app key is wrong, or the connection is not active. The SDK keeps the events and stops sending until the app launches again; fix the key in `configure`.
- **`Herald needs a user token: call setUserToken`.** Identity verification is **Required** and the signed-in user's token is missing or wrong. The SDK keeps that user's events and sends them after the next `setUserToken`.
- **`Firebase is not initialized; push token not synced`.** `google-services.json` is missing or the Google services plugin is not applied.
- **No cards.** Cards are fetched for identified users only. An agent send made before the user's device first reached Herald is suppressed, so send again after the app has run once.

## Next

- [Users and identity](/docs/concepts/users-and-identity)
- [Android push setup](/docs/push-notifications/android-setup)
- [Content cards](/docs/content-cards/overview)
