# Deep links

> Open a specific screen in your app when a user taps a push notification, a content card or an in-app message button.

A deep link is a URL that opens a specific screen in your app, such as `lumen://plan/today`. Every Herald push, content card and in-app button can carry one; the SDK opens it when the user taps.

## Before you start

- A URL scheme or universal link (iOS) or App Link (Android) your app already handles, or will handle after this guide.
- Notification taps wired to the SDK: [iOS setup](/docs/push-notifications/ios-setup#5-handle-taps-and-foreground-display) or [Android setup](/docs/push-notifications/android-setup#6-handle-taps).

## How the SDK opens links

| Source | iOS | Android |
|---|---|---|
| Push tap | `handleNotificationResponse` opens the push's `deeplink` (a tapped action button's own link first) | `handleNotificationIntent` opens the push's `deeplink` (a tapped action button's own link first) |
| Content card or in-app message tap | The `HeraldUI` views call `openDeepLink(_:)` for the card or button URL; custom views call it themselves | The `herald-ui` views open the card or button URL; custom views call `Herald.openUrl` |
| How it opens | Your `deepLinkHandler`, or the system when the handler is not set or returns `false` | Your `Herald.deepLinkHandler`, or an `ACTION_VIEW` intent for the URL when the handler is not set or returns `false` |

Push dismissals never open a link.

## 1. Route links in your app

:::tabs
::tab ios
Set `deepLinkHandler` once at launch. Return `true` when your app handled the URL; return `false` to let the SDK pass it to the system (for example a web URL that should open in Safari).

```swift
@MainActor
func installDeepLinkHandler() {
    Herald.shared.deepLinkHandler = { url in
        guard url.scheme == "lumen" else { return false }
        Router.shared.open(url) // your app's navigation
        return true
    }
}
```

Handle your own universal links (`https://` URLs on your domain) in the handler too, and return `true`. Handing them to the system from inside your app is not a reliable way to open your own screens.

To open a link from your own UI the same way, for example from a custom card list, call `Herald.shared.openDeepLink(url)`.

From SDK 0.6.0 every link that goes through the SDK (both platforms) is recorded as a `$deep_link_opened` event, without its query unless you turn on `trackDeepLinkQuery`. See [Lifecycle events](/docs/reference/event-payload#lifecycle-events).
::tab android
Set `Herald.deepLinkHandler` once, in `Application.onCreate`. Return `true` when your app handled the URL; return `false` to let the SDK open it with `Intent.ACTION_VIEW` (for example a web URL that should open in the browser). Taps and clicks call it on the main thread.

```kotlin
Herald.deepLinkHandler = { url ->
    if (url.startsWith("lumen://")) {
        Router.open(url) // your app's navigation, inside the current task
        true
    } else {
        false
    }
}
```

With a handler, in-app buttons and card clicks navigate inside your running activity instead of starting a new task. A push tap still arrives through `handleNotificationIntent` in your launch activity, which passes the link to the handler.

Without a handler, or for links it returns `false` for, declare an intent filter for your scheme on the activity that shows the screen. The SDK opens those links with `Intent.ACTION_VIEW` in a new task, so any activity that matches receives them.

```xml
<activity
    android:name=".MainActivity"
    android:exported="true"
    android:launchMode="singleTop">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="lumen" />
    </intent-filter>
</activity>
```

Read the link in `onCreate` and `onNewIntent` from `intent.data`. With `singleTop`, a link that arrives while the activity is on top reaches `onNewIntent`.

For `https://` links on your domain, handle them in `deepLinkHandler` and return `true`, or set up Android App Links so they open your app instead of the browser. To open a link from your own UI the same way, call `Herald.openUrl(url)`.
:::

## 2. Add the link to the message

In Herald, set the deep link on the push message, on the content card, or on each in-app message button. Use the full URL, including the scheme.

## Verify it works

:::tabs
::tab ios
Open the link in the simulator to test your routing on its own:

```bash
xcrun simctl openurl booted "lumen://plan/today"
```
::tab android
Open the link on a connected device or emulator to test your routing on its own:

```bash
adb shell am start -a android.intent.action.VIEW -d "lumen://plan/today"
```
:::

Then send a test push with the same deep link and tap it. See [Testing push](/docs/push-notifications/testing).

## Troubleshooting

- **iOS opens Safari instead of the app.** The handler returned `false` for a URL your app owns, or no handler is set. Return `true` for your scheme and your domains.
- **Nothing happens on Android.** No `deepLinkHandler` took the link and no activity declares an intent filter for the scheme. Logcat shows `openUrl failed` under the tag `Herald`.
- **Android opens a second copy of your screen.** The link went to `ACTION_VIEW`, which starts a new task. Set `Herald.deepLinkHandler` and navigate inside your app.
- **The link opens, but on the wrong screen.** Your routing reads the URL before the app finishes launching. Store the URL and navigate once your root view is ready.
- **A web link opens in the app instead of the browser.** Your universal link or App Link configuration claims that path. Narrow the paths your app claims.

## Next

- [Silent push](/docs/push-notifications/silent-push)
- [Testing push](/docs/push-notifications/testing)
