# 푸시 알림 데이터 이용

앱이 포그라운드 상태일 때 화면에 알림 메시지가 보여지도록 구현하지 않은 이상 화면에 배너 형태로 알림 메시지가 보이지 않습니다.

앱이 포그라운드 상태인데 화면에 배너 형태로 알림 메시지가 보인다면 이미 고객님께서 알림 메시지가 보여지도록 구현한 것입니다. 이 경우, 해당 구현부분 상단에 아래의 내용을 참조하여 수정해주셔야 합니다.

알림 메시지가 보이지 않거나 Delegation 메쏘드가 구현되지 않은 경우에는 아래 내용 참고하셔서 해당 메쏘드를 추가하여 데이터를 추출할 수 있습니다.

### 딥링크 데이터 이용시 주의사항

딥링크 데이터를 이용할 때 아래와 같은 점에 유의해주세요.

1. 앱이 Foreground상태일 때 딥링크 URL로 화면이 바로 이동하면 이용자가 당황스러울 수 있습니다. 이에 대해 기획단계부터 어떻게 할 것인지 고민해주세요. 보통은 로컬 노티피케이션을 한 번 더 띄우기도 합니다.
2. 앱의 세 가지 상태 (Foreground, Background, Killed)에 따라 작동 매카니즘이 다릅니다. 각 상태별로 호출 구조를 체크해주세요.

위에서 딥링크와 관련해서 안내드렸는데요, 푸시를 통해 전달되는 데이터를 이용하는 것에 모두 해당되는 내용입니다.

### 앱이 포그라운드 상태일 때

AppDelegate 의 "userNotificationCenter(\_:willPresent:withCompletionHandler:):" 메쏘드가 호출됩니다.&#x20;

{% tabs %}
{% tab title="Swift" %}

```swift
// UNUserNotificationCenterDelegate 프로토콜 구현
@main
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
  ...
  func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    print("userInfo: \(notification.request.content.userInfo)")
    if let rootDict = notification.request.content.userInfo as NSDictionary? {
      if let jsonDict = rootDict["RW_push_payload_WP"] as? NSDictionary {
        let title = jsonDict["RW_push_payload_TT"]
        let body = jsonDict["RW_push_payload_BD"]
        let deepLink = jsonDict["RW_push_payload_deeplink"]
        
        print("Notification title: \(title), body: \(body), deepLink: \(deepLink)")
      }
    }
    completionHandler([.sound, .badge])
  }
  ...
}
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
// 앱 포그라운드 상태일 때 알림
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler  API_AVAILABLE(ios(10.0)) {
    NSDictionary *userInfo = notification.request.content.userInfo;
    NSLog(@"푸시 데이터 : %@", userInfo);

    NSDictionary *jsonDict = [dictionary objectForKey:@"RW_push_payload_WP"];
    if (jsonDict != nil) {
        String *title = [jsonDict objectForKey:@"RW_push_payload_TT"];
        String *body = [jsonDict objectForKey:@"RW_push_payload_BD"];
        String *deepLink = [jsonDict objectForKey:@"RW_push_payload_deeplink"];
        NSLog("Notification title: %@, body: %@, deepLink: %@", title, body, deepLink);
    }

    completionHandler(UNNotificationPresentationOptionNone);    // 포그라운드 상태에서 푸시왔을 때 푸시 마노출
//  completionHandler(UNNotificationPresentationOptionAlert);   // 포그라운드 상태에서 푸시왔을 때 푸시 노출
}
```

{% endtab %}
{% endtabs %}

### jsonDict 에 포함된 데이터 Key/Value

jsonDict 는 일반 문자열입니다. 해당 문자열은 JSON 형식으로 되어있으니 사용하시는 JSON Parser를 이용하여 JSON 객체로 전환하여 사용해주세요.

* **최상위 객체의 구조**

<table><thead><tr><th width="315">최상위 Payload 키</th><th width="324.3333333333333">Payload 값</th><th>필수여부</th></tr></thead><tbody><tr><td>RW_push_payload_WP</td><td>Sub객체 (아래  Sub객체 참조)</td><td>필수</td></tr></tbody></table>

* Sub객체의 구조

<table><thead><tr><th width="313.5916083661996">Payload 키</th><th width="201.09554020650634">Payload 값</th><th>필수여부</th></tr></thead><tbody><tr><td>RW_push_payload_TT</td><td>메시지 타이틀 </td><td>필수</td></tr><tr><td>RW_push_payload_BD</td><td>메시지 내용</td><td>필수</td></tr><tr><td>RW_push_payload_IM</td><td>이미지 URL</td><td>X</td></tr><tr><td>RW_push_payload_MV</td><td>유튜브 동영상 URL</td><td>X</td></tr><tr><td>RW_push_payload_deeplink</td><td>딥링크 URL</td><td>X</td></tr></tbody></table>

### 앱이 백그라운드 상태일 때

앱이 백그라운드이거나 Killed 된 상태인 경우, 위의 메쏘드가 호출되지 않습니다.

알림 메시지는 앱이 백그라운드 상태에서 알림 표시줄에 배너 형태 등으로 표시됩니다. 해당 알림 메시지를 탭하면 아래와 같은 "userNotificationCenter(\_:didReceive:withCompletionHandler:)" 메쏘드가 호출됩니다. 해당 메쏘드에서 데이터를 추출하여 사용합니다.

{% tabs %}
{% tab title="Swift" %}

```swift
// UNUserNotificationCenterDelegate 프로토콜 구현
@main
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
  ...
  func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    print("userInfo: \(response.notification.request.content.userInfo)")
    if let rootDict = response.notification.request.content.userInfo as NSDictionary? {
      if let jsonDict = rootDict["RW_push_payload_WP"] as? NSDictionary {
        let title = jsonDict["RW_push_payload_TT"]
        let body = jsonDict["RW_push_payload_BD"]
        let deepLink = jsonDict["RW_push_payload_deeplink"]
        
        print("Notification title: \(title), body: \(body), deepLink: \(deepLink)")
      }
    }
    completionHandler()
  }
  ...
}
```

{% endtab %}

{% tab title="Objective-C" %}

```objectivec
// 앱 백라운드 상태일 때 알림
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler  API_AVAILABLE(ios(10.0)){
    NSDictionary *userInfo = response.notification.request.content.userInfo;
    NSLog(@"푸시 데이터 : %@", userInfo);

    NSDictionary *jsonDict = [dictionary objectForKey:@"RW_push_payload_WP"];
    if (jsonDict != nil) {
        String *title = [jsonDict objectForKey:@"RW_push_payload_TT"];
        String *body = [jsonDict objectForKey:@"RW_push_payload_BD"];
        String *deepLink = [jsonDict objectForKey:@"RW_push_payload_deeplink"];
        NSLog("Notification title: %@, body: %@, deepLink: %@", title, body, deepLink);
    }

    completionHandler();
}
```

{% endtab %}
{% endtabs %}


---

# Agent Instructions: Querying This Documentation

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

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://document.wisetracker.co.kr/v2-developer/push/undefined/ios/notification-data.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
