> ## Documentation Index
> Fetch the complete documentation index at: https://docs.hightouch.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Set up Android push notifications

> The android-push library connects your Android app to Hightouch Delivery so campaigns and journeys can send it push notifications. Register your app in Hightouch, add the SDK to your app, and send yourself a test.

Setting up iOS too? See [Set up iOS push notifications](https://docs.hightouch.com/native-delivery/push-ios.md).

## Before you begin

* **Delivery** — Delivery must be enabled in your workspace.
* **Parent model** — Delivery must have a parent model selected in the **Data source** card on **Delivery > Settings > Profiles**. Push notifications are addressed to users by its primary key.
* **Firebase project** — Your app must be registered in a Firebase project, and you need access to its **Project settings** to create a service account key.
* **Test device** — Use a device or an emulator with Google Play services.
* **Android 5.0 or later** — The SDK supports API level 21 and later.

## Collect your Firebase credentials

Hightouch sends notifications through Firebase Cloud Messaging (FCM) on your behalf, using two values from your Firebase project.

| Credential | Where to find it |
| - | - |
| **Package name** | Your app's `applicationId`, in the app module's `build.gradle` or `build.gradle.kts` file. |
| **FCM service account JSON** | In the [Firebase console](https://console.firebase.google.com/), go to **Project settings > Service accounts** and click **Generate new private key**. |

The service account key is a private credential. Upload it to Hightouch and don't commit it to your app's repository.

## Create an Android event source

The SDK sends device registrations and notification opens to a Hightouch event source of type Android. Skip this section if your workspace already has one.

1. Go to **Event Collection > Event sources** and click **Create source**.
2. Click **Android** and finish creating the source.
3. On the source's **Setup** tab, copy the **Write key** and the **API Host**.

A workspace can have only one Android event source for push.

## Register your app in Hightouch

1. Go to **Delivery > Settings > Apps**. If you already registered this app for iOS, open it and skip to the next step. Otherwise, click **Add**, enter a **Name**, and click **Add**. The name appears on every notification.
2. On the **Android (FCM)** card, click **Enable**.
3. Enter your [Firebase credentials](#collect-your-firebase-credentials), then save.
4. Copy the **App ID** from the top of the **Android (FCM)** card.

Hightouch connects the app to your event source when you enable it.

> [!WARNING]
>
> You can't disable a platform after you enable it. To delete an app, contact Hightouch support.

> [!NOTE]
>
> The App ID isn't your app's package name or bundle ID. It's an ID that Hightouch assigns.

## Configure your Android project

### Add Firebase to your app

If your app doesn't use Firebase yet, follow [Add Firebase to your Android project](https://firebase.google.com/docs/android/setup): download `google-services.json` into your app module and apply the Google Services Gradle plugin. The Hightouch SDK includes Firebase Cloud Messaging, so you don't need to add it separately.

### Install the SDK

The SDK is published on JitPack. Add the JitPack repository in `settings.gradle.kts`:

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

Then add the SDK to your app module's `build.gradle.kts`:

```kotlin theme={null}
dependencies {
    implementation("com.github.ht-sdks.events-sdk-kotlin:android-push:0.0.2")
}
```

This also brings in the Hightouch [Android SDK](https://docs.hightouch.com/events/sdks/android.md) for events.

### Set a notification icon

Android needs a small icon to display a notification. Declare one in `AndroidManifest.xml`, inside `<application>`:

```xml theme={null}
<meta-data
    android:name="com.hightouch.push.default_notification_icon"
    android:resource="@drawable/ic_notification" />
```

Use a white, transparent-background icon. Without one, the SDK falls back to your app's launcher icon. If neither exists, the notification isn't shown.

### Declare the notification permission

Android 13 and later require the `POST_NOTIFICATIONS` permission to show notifications. Add it to `AndroidManifest.xml`, outside `<application>`:

```xml theme={null}
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
```

## Initialize the SDK

Initialize `HightouchPush` in your `Application` class's `onCreate` with your write key, API host, and App ID. Android can start your app in the background to deliver a push, so initializing here makes sure the SDK is ready.

```kotlin theme={null}
import android.app.Application
import com.hightouch.analytics.kotlin.android.Analytics
import com.hightouch.analytics.kotlin.push.HightouchPush
import com.hightouch.analytics.kotlin.push.HightouchPushConfig

class MyApplication : Application() {
    override fun onCreate() {
        super.onCreate()

        val analytics = Analytics("YOUR_WRITE_KEY", this) {
            apiHost = "YOUR_API_HOST/v1"
            cdnHost = "YOUR_API_HOST/v1"
        }

        HightouchPush.initialize(
            analytics = analytics,
            config = HightouchPushConfig.Builder("YOUR_HIGHTOUCH_APP_ID").build(),
        )
    }
}
```

Replace `YOUR_API_HOST` with the **API Host** from your event source's **Setup** tab, such as `eu-west-1.hightouch-events.com`, and keep `/v1` at the end.

If you already create an `Analytics` instance to track events, pass that one to `HightouchPush.initialize` instead of creating a second. Its write key must belong to your Android event source.

The SDK registers the device with FCM on its own. You don't need to fetch or forward the token.

`HightouchPushConfig.Builder` takes these options:

| Option | What it does | Default |
| - | - | - |
| **`setUrlHandler`** | Handles links from notification taps. See [Handle deep links](#handle-deep-links). | None |
| **`setCustomActionHandler`** | Handles action buttons that don't open a URL. | None |
| **`setSilentPushListener`** | Receives data from silent pushes. See [Receive silent pushes](#receive-silent-pushes). | None |
| **`setAllowedProtocols`** | URL schemes other than `https` that the SDK can open, such as `listOf("myapp")`. | Empty |
| **`setNotificationChannelId`** | The notification channel for Hightouch pushes. See [Choose a notification channel](#choose-a-notification-channel). | `hightouch_default` |
| **`setSmallIconResId`** | The notification icon, if you don't set one in the manifest. | None |
| **`setNotificationColorResId`** | The accent color for notifications. | None |
| **`setAutoLaunchApp`** | Opens your app when a user taps a notification that has no link. | `true` |

## Request notification permission

On Android 13 and later, ask the user for permission before you expect notifications to show. You decide when to ask; many apps wait until after onboarding. This example requests it from an activity:

```kotlin theme={null}
private val requestNotificationPermission =
    registerForActivityResult(ActivityResultContracts.RequestPermission()) { }

private fun askForNotificationPermission() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU &&
        ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) !=
        PackageManager.PERMISSION_GRANTED
    ) {
        requestNotificationPermission.launch(Manifest.permission.POST_NOTIFICATIONS)
    }
}
```

If the user declines, the device stays registered but notifications don't appear.

## Identify users on login and logout

Call `identify` when a user logs in and `logout` when they log out:

```kotlin theme={null}
HightouchPush.identify(userId = "USER_ID")

HightouchPush.logout()
```

The user ID must match the user's primary key in your Delivery parent model. Delivery uses it to find each user's devices. `identify` links the device to that user, and `logout` unlinks it so the device stops receiving their notifications.

If you also track events with the Android SDK, call `HightouchPush.identify` instead of `analytics.identify` so the device stays linked to the right user.

## Handle notification taps

The SDK handles taps on its own. It records the open, follows the notification's link, and opens your app when there's no link.

To read a notification's custom data in the activity it opens, call `HightouchPush.getCustomData(intent)` in `onCreate` or `onNewIntent`.

### Handle deep links

By default, the SDK opens `https` links with Android's standard link handling, which uses your app's App Links when they match. To route links yourself, set a URL handler and return `true` for links you handle:

```kotlin theme={null}
val config = HightouchPushConfig.Builder("YOUR_HIGHTOUCH_APP_ID")
    .setUrlHandler { url, context -> router.open(url) }
    .setAllowedProtocols(listOf("myapp"))
    .build()
```

To let the SDK open links with a custom scheme, such as `myapp://`, add the scheme to `setAllowedProtocols`. Keep the handler fast, since it runs on the main thread.

### Use the SDK alongside another FCM provider

If your app already has its own `FirebaseMessagingService`, Android delivers messages to yours instead of the SDK's. Forward new tokens and messages to the SDK:

```kotlin theme={null}
class MyMessagingService : FirebaseMessagingService() {
    override fun onNewToken(token: String) {
        HightouchFirebaseMessagingService.handleTokenRefresh(token)
    }

    override fun onMessageReceived(message: RemoteMessage) {
        if (!HightouchFirebaseMessagingService.handleMessageReceived(this, message)) {
            // Not a Hightouch push. Handle it as before.
        }
    }
}
```

`handleMessageReceived` returns `false` for messages that didn't come from Hightouch.

## Choose a notification channel

Android groups notifications into channels that users can turn on and off in system settings. By default, Hightouch pushes use a channel named "Push notifications" that the SDK creates for you.

To use your own channel, create it in your app, then pass its ID to `setNotificationChannelId`. A channel's sound and importance come from the channel, not from the campaign.

## Receive silent pushes

A silent push delivers custom data to your app without showing a notification. Set a listener on your config:

```kotlin theme={null}
val config = HightouchPushConfig.Builder("YOUR_HIGHTOUCH_APP_ID")
    .setSilentPushListener { customData ->
        // Refresh content or sync state.
    }
    .build()
```

The listener runs on a background thread, so it can do short work directly.

## Send yourself a test push

Run your app on a device and log in so that `identify` runs, then send a test from Hightouch. In both places, **Validate** checks that a device is registered for the user ID you enter.

**From app settings:** Go to **Delivery > Settings > Apps**, open your app, and click **Test**. Enter your user ID, click **Validate**, then click **Send test**. The test includes an image, so it also checks that rich notifications display.

**From the campaign editor:** Go to **Delivery > Campaigns**, click **Add campaign**, and select **Push**. In the **Create a new campaign** dialog, select your app under **Choose apps to target**, choose a brand if asked, and click **Continue**. Under **Content**, click **Edit** and write your message. On the **Preview** tab, click **Send test**, enter your user ID, click **Validate**, then click **Send test**.

If **Validate** finds no device, you can enter a device token instead. A test that arrives this way means your push credentials work and the device isn't linked to that user.

## Troubleshooting

#### No active devices found

**Validate** shows this when no device is registered for the user ID. Check that:

* `appId` is the App ID from your app's **Android (FCM)** card.
* Your app calls `identify` after the user logs in, with the parent model's primary key.
* The API host in your SDK setup matches the **API Host** on your Android event source's **Setup** tab.

After you fix any of these, call `identify` again on the device. To see what your app sends, open your Android event source's **Debugger** tab. After `identify`, you see a `CEP Push Token Events` event with your user ID and an `app_id` that matches your App ID.

If your app has its own `FirebaseMessagingService`, also check that it [forwards tokens to the SDK](#use-the-sdk-alongside-another-fcm-provider).

#### Validate passes, but no notification appears

Check that the user granted notification permission on Android 13 or later, and that notifications for your app and its channel are turned on in system settings.

#### No android event source found for this workspace

You see this when you enable the **Android (FCM)** card before your workspace has an Android event source. [Create an Android event source](#create-an-android-event-source), then click **Enable** again. If your workspace has more than one Android event source, contact Hightouch support.

## Next steps

* [Preview and test](https://docs.hightouch.com/native-delivery/preview-and-test.md) to render a message against real profiles before you send it.
* [Send a campaign](https://docs.hightouch.com/native-delivery/campaigns.md) to schedule your first push to an audience.
