> 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/development-guide/react-native/in-app-message/react-native-in-app-message-listener.md).

# 인앱메시지 이벤트 리스너

{% hint style="info" %}
React Native SDK 3.15.0 이상 버전에서 지원하는 기능입니다.
{% endhint %}

인앱 메시지가 열리고 닫히는 시점과 클릭 시점에 앱 로직을 실행할 수 있습니다.

## 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;
}
```

모든 메서드는 선택 구현입니다.

<table><thead><tr><th width="250">메서드</th><th>호출 시점</th></tr></thead><tbody><tr><td><code>beforeInAppMessageOpen</code></td><td>인앱 메시지가 열리기 직전</td></tr><tr><td><code>afterInAppMessageOpen</code></td><td>인앱 메시지가 열린 후</td></tr><tr><td><code>beforeInAppMessageClose</code></td><td>인앱 메시지가 닫히기 직전</td></tr><tr><td><code>afterInAppMessageClose</code></td><td>인앱 메시지가 닫힌 후</td></tr><tr><td><code>onInAppMessageClick</code></td><td>인앱 메시지의 버튼이나 이미지를 클릭했을 때</td></tr></tbody></table>

#### onInAppMessageClick

`onInAppMessageClick`의 반환값으로 SDK가 클릭 액션을 실행할지 정할 수 있습니다.\
클릭 이벤트(`$in_app_action`)는 반환값과 관계없이 수집됩니다.

* `true`: SDK가 클릭 액션을 실행하지 않습니다.
  * 예를 들어 클릭 액션이 "하루 동안 보지 않기"이면 메시지를 숨기지 않습니다.
  * 클릭 액션이 "링크 이동"이면 링크를 열지 않고, [HackleDelegate로 링크 처리](https://docs.hackle.io/development-guide/react-native/react-native-deeplink/react-native-deeplink-delegate.md)의 `HackleDelegate`를 호출하지 않습니다.
* `false`: SDK가 설정된 클릭 액션을 그대로 실행합니다.

{% hint style="danger" %}
`onInAppMessageClick`은 반환값을 동기로 돌려줘야 합니다.\
SDK는 반환값을 최대 100ms 기다리고, 응답이 없으면 `false`로 처리해 클릭 액션을 실행합니다.\
시간이 걸리는 작업은 반환한 뒤에 따로 실행하세요.
{% endhint %}

{% hint style="warning" %}
`onInAppMessageClick` 안에서 `view.close()`를 호출하면 `false`를 반환해도 `true`를 반환한 것과 같이 처리됩니다.
{% endhint %}

### HackleInAppMessage

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

* 인앱 메시지의 키를 반환합니다.

### HackleInAppMessageView

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

* `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>클릭 액션의 종류입니다. 닫기 액션은 <code>'CLOSE'</code>, 링크 이동 액션은 <code>'LINK'</code>입니다.</td></tr><tr><td><code>close?.hideDurationMillis</code></td><td>메시지를 특정 기간 동안 숨기는 액션인 경우 해당 기간을 밀리초로 반환합니다.</td></tr><tr><td><code>link?.url</code></td><td>링크가 포함된 경우 링크를 반환합니다. e.g) https://hackle.io</td></tr><tr><td><code>link?.shouldCloseAfterLink</code></td><td>"클릭시 메시지 닫기" 옵션이 켜진 경우 <code>true</code>를 반환합니다.</td></tr></tbody></table>

## 사용 예제 <a href="#usage-example" id="usage-example"></a>

### 리스너 등록 <a href="#register-listener" id="register-listener"></a>

`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) {
      // 99번 인앱 메시지의 클릭만 앱에서 직접 처리합니다.
      view.close();
      return true;
    }
    return false;
  },
});
```

### 리스너 해제 <a href="#unregister-listener" id="unregister-listener"></a>

`setInAppMessageListener`에 `null`을 전달하면 등록된 리스너를 해제할 수 있습니다.

```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/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.
