# Android setup

> Connect Herald to Firebase Cloud Messaging with a service account, add the FCM module and handle notification taps.

This guide connects Herald to Firebase Cloud Messaging (FCM) and wires your app to register for push, show notifications and report opens.

## Before you start

- A Firebase project with your Android app registered under its package name.
- The SDK installed and configured. See [Quickstart: Android](/docs/overview/quickstart-android).
- Admin access to the Herald sending connection your agents use.

## 1. Create a service account key

1. In the Firebase console, open **Project settings > Service accounts** and click **Generate new private key**. This downloads a JSON file for the project's Firebase Admin SDK service account, which can send through the FCM HTTP v1 API.
2. Make sure the **Firebase Cloud Messaging API (V1)** is enabled for the project (Project settings > Cloud Messaging).

## 2. Add it to Herald

In Herald, open **Settings > Sending**, open the connection and paste the JSON into the FCM section. Herald reads the project ID from the file and stores the key encrypted. Delete the downloaded copy afterwards.

## 3. Add Firebase and the FCM module

Add `google-services.json` to your app module, apply the Google services plugin and add `herald-fcm`. If your own code uses Firebase Messaging classes (for example your own messaging service), add `com.google.firebase:firebase-messaging` too: `herald-fcm` does not expose it.

```kotlin
// 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-fcm:0.6.0")
}
```

`herald-fcm` registers `HeraldMessagingService` in its manifest. The service passes every new FCM token to `Herald.setPushToken` and shows Herald pushes that reach your app while it is in the foreground.

## 4. Start push in your Application class

```kotlin
class LumenApplication : Application() {
    override fun onCreate() {
        super.onCreate()
        Herald.configure(this, appKey = "YOUR_APP_KEY", endpoint = "https://YOUR_HERALD_HOST")
        HeraldNotifications.createDefaultChannel(this)
        HeraldMessagingService.syncToken(this)
    }
}
```

`syncToken` fetches the current FCM token, so a device that got its token before you added the SDK is registered too. It does nothing when Firebase is not initialized.

## 5. Show notifications in the background

When your app is in the background, Android displays Herald pushes itself, without calling your code. Point Firebase's defaults at the Herald channel and at your notification icon so both paths look the same:

```xml
<application>
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_channel_id"
        android:value="herald_default" />
    <meta-data
        android:name="com.google.firebase.messaging.default_notification_icon"
        android:resource="@drawable/ic_notification" />
</application>
```

`herald-fcm` uses the same icon setting for foreground notifications and falls back to your app icon. Use a white, transparent icon: Android draws the small icon as a silhouette.

## 6. Handle taps

A tap opens your launch activity with the Herald extras on the intent. Pass the intent to the SDK from `onCreate` and `onNewIntent`. It logs the open, evaluates in-app messages with a push open trigger, opens the deep link, and removes the extras so a recreated activity does not count the open twice.

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

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

Declare the launch activity with `android:launchMode="singleTop"` so a tap while the app is open reaches `onNewIntent` instead of stacking a second copy.

## If your app has its own messaging service

Android delivers FCM messages to one service. If you already have one, remove the Herald service from the merged manifest and call the SDK from yours:

```xml
<service android:name="com.herald.fcm.HeraldMessagingService" tools:node="remove" />
```

```kotlin
class LumenMessagingService : FirebaseMessagingService() {
    override fun onNewToken(token: String) {
        Herald.setPushToken(token)
    }

    override fun onMessageReceived(message: RemoteMessage) {
        if (HeraldNotifications.handleRemoteMessage(this, message)) return
        // Handle your other pushes here.
    }
}
```

You can instead subclass `HeraldMessagingService`, override `onNonHeraldMessage`, and register your subclass in place of the original.

## What Herald sends

Each push is an FCM notification message with high Android priority:

| Field | Contents |
|---|---|
| `notification.title`, `notification.body` | The title (optional) and body |
| `notification.image` | The message's Android image, when it has one |
| `data.nexus_decision_id` | The Herald decision ID. The SDK uses it to recognize Herald pushes and credit the open. |
| `data.deeplink` | The message's deep link, when it has one |

## Verify it works

1. Run the app on a device or an emulator with Google Play services, sign in so the user is identified, and allow notifications.
2. With `LogLevel.DEBUG`, Logcat (tag `Herald`) shows `Sent N events` after the token is registered. A `Firebase is not initialized` warning means step 3 is incomplete.
3. Send a test push. See [Testing push](/docs/push-notifications/testing).

## Troubleshooting

- **FCM returns `SENDER_ID_MISMATCH`.** The service account belongs to a different Firebase project than the app's `google-services.json`.
- **FCM returns `UNREGISTERED`.** The app was uninstalled or the token was replaced. The SDK sends new tokens as FCM issues them.
- **Foreground pushes do not show.** Another messaging service receives the messages. Remove one of them, or call `HeraldNotifications.handleRemoteMessage` from yours.
- **Taps are not counted.** `Herald.handleNotificationIntent` is missing from `onCreate` or `onNewIntent` of the launch activity.

## Next

- [Permission prompts](/docs/push-notifications/permission-prompts)
- [Android notification channels](/docs/push-notifications/android-notification-channels)
- [Deep links](/docs/push-notifications/deep-links)
