> For the complete documentation index, see [llms.txt](https://docs.hackle.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hackle.io/en/development-guide/react-native/in-app-message/react-native-in-app-message-listener.md).

# In-App Message Event Listener

{% hint style="info" %}
This feature is supported in React Native SDK version 3.15.0 and above.
{% endhint %}

You can run app logic when an In-App Message opens, closes, or is clicked.

## Interfaces

### HackleInAppMessageListener

```typescript
interface HackleInAppMessageListener {
  beforeInAppMessageOpen?(inAppMessage: HackleInAppMessage): void;
  afterInAppMessageOpen?(inAppMessage: HackleInAppMessage): void;
  beforeInAppMessageClose?(inAppMessage: HackleInAppMessage): void;
  afterInAppMessageClose?(inAppMessage: HackleInAppMessage): void;
  onInAppMessageClick?(
    inAppMessage: HackleInAppMessage,
    view: HackleInAppMessageView,
    action: HackleInAppMessageAction
  ): boolean;
}
```

All methods are optional.

<table><thead><tr><th width="250">Method</th><th>When Called</th></tr></thead><tbody><tr><td><code>beforeInAppMessageOpen</code></td><td>Right before the In-App Message opens</td></tr><tr><td><code>afterInAppMessageOpen</code></td><td>After the In-App Message opens</td></tr><tr><td><code>beforeInAppMessageClose</code></td><td>Right before the In-App Message closes</td></tr><tr><td><code>afterInAppMessageClose</code></td><td>After the In-App Message closes</td></tr><tr><td><code>onInAppMessageClick</code></td><td>When a button or image in the In-App Message is clicked</td></tr></tbody></table>

#### onInAppMessageClick

The return value of `onInAppMessageClick` determines whether the SDK runs the click action.\
The click event (`$in_app_action`) is collected regardless of the return value.

* `true`: The SDK does not run the click action.
  * For example, if the click action is "Do not show for a day", the message is not hidden.
  * If the click action is "Link", the SDK does not open the link and does not call the `HackleDelegate` described in [Handle Links with HackleDelegate](https://docs.hackle.io/en/development-guide/react-native/react-native-deeplink/react-native-deeplink-delegate.md).
* `false`: The SDK runs the configured click action as is.

{% hint style="danger" %}
`onInAppMessageClick` must return its value synchronously.\
The SDK waits up to 100ms for the return value. If there is no response, it treats the result as `false` and runs the click action.\
Run time-consuming work separately after returning.
{% endhint %}

{% hint style="warning" %}
If you call `view.close()` inside `onInAppMessageClick`, it is treated the same as returning `true`, even if you return `false`.
{% endhint %}

### HackleInAppMessage

```typescript
interface HackleInAppMessage {
  key: number;
}
```

* Returns the key of the In-App Message.

### HackleInAppMessageView

```typescript
interface HackleInAppMessageView {
  close(): void;
}
```

* You can close the In-App Message within the listener by calling `close()`.

### HackleInAppMessageAction

```typescript
type HackleInAppMessageActionType = 'CLOSE' | 'LINK';

interface HackleInAppMessageAction {
  type: HackleInAppMessageActionType;
  close?: {
    hideDurationMillis: number | null;
  };
  link?: {
    url: string;
    shouldCloseAfterLink: boolean;
  };
}
```

<table><thead><tr><th width="250">property</th><th>description</th></tr></thead><tbody><tr><td><code>type</code></td><td>The type of click action. A close action is <code>'CLOSE'</code> and a link action is <code>'LINK'</code>.</td></tr><tr><td><code>close?.hideDurationMillis</code></td><td>If the action hides the message for a specific period, returns that duration in milliseconds.</td></tr><tr><td><code>link?.url</code></td><td>Returns the link if one is included. e.g) https://hackle.io</td></tr><tr><td><code>link?.shouldCloseAfterLink</code></td><td>Returns <code>true</code> if the "Close message on click" option is turned on.</td></tr></tbody></table>

## Usage Example

### Registering a Listener

Register a listener with `setInAppMessageListener`.

```typescript
hackleClient.setInAppMessageListener({
  afterInAppMessageOpen(inAppMessage) {
    console.log('afterInAppMessageOpen key:', inAppMessage.key);
  },
  afterInAppMessageClose(inAppMessage) {
    console.log('afterInAppMessageClose key:', inAppMessage.key);
  },
  onInAppMessageClick(inAppMessage, view, action) {
    if (inAppMessage.key === 99) {
      // Handle clicks only for In-App Message 99 directly in the app.
      view.close();
      return true;
    }
    return false;
  },
});
```

### Removing a Listener

Pass `null` to `setInAppMessageListener` to remove the registered listener.

```typescript
hackleClient.setInAppMessageListener(null);
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.hackle.io/en/development-guide/react-native/in-app-message/react-native-in-app-message-listener.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
