> For the complete documentation index, see [llms.txt](https://app.developers.karte.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://app.developers.karte.io/ios-sdk-appendix/appendix-rich-notification-ios-sdk.md).

# \[iOS]リッチプッシュ通知に対応する

リッチプッシュ通知に対応することで、画像や動画などを添付したプッシュ通知を受信することが可能です（本機能は iOS10 以上でのみ利用可能です）\
※リッチプッシュ通知はiOS独自の概念です。Andoridで同等のプッシュ通知を実現したい場合は[通知ペイロードのカスタマイズ](/app-send-notification/app-customize-notification-payload.md) 等を組み合わせて独自に実装する必要があります。

## Extensionの作成

リッチプッシュ通知を受信するために、Notification Service Extension を作成する必要があります。

### ターゲットを追加する

Xcodeのメニューの `File` > `New` > `Target...` をクリックします。

### テンプレートを選択する

`iOS` > `Notification Service Extension` を選択し、`Next` をクリックします。

![ios-extension-setup-1](/files/ZBQ9WAklpid07Vat4E2d)

### ターゲットのオプションを設定する

以下の項目を入力した上で、`Finish` をクリックします。

* Product Name
* Organization Name
* Organization Identifier

![ios-extension-setup-2](/files/08CpeTRfvsWzSo6tHQEO)

`Activate` をクリックします。

![ios-extension-setup-3](/files/bXJK6i0YMp7FWpYi1Qsr)

{% hint style="warning" %}
**deployment targetについて**

追加したExtensionのdeployment targetは本体のターゲットと同一にする必要があります。\
追加時の設定のままだと、最新版に近いバージョンが指定されるため、古いOSバージョンではリッチプッシュの表示のみできない等の不具合が起こる可能性があります。
{% endhint %}

## SDK のセットアップ

### CocoaPods を利用してセットアップする

#### Podfile の編集

Podfile を任意のエディタで開き、App Extensions SDK の情報を追記します。\
`TARGET_NAME` 部分は、Extension 作成時に指定した `Product Name` に置き換えてください。

{% code title="Podfile" overflow="wrap" %}

```ruby
target 'TARGET_NAME' do
  pod 'KarteNotificationServiceExtension'
end
```

{% endcode %}

#### SDK をインストール

以下のコマンドをターミナルで実行し、SDK をインストールします。

{% code overflow="wrap" %}

```bash
pod install
```

{% endcode %}

### Swift Package Manager を利用してセットアップする

Xcode より公式githubレポジトリ(<https://github.com/plaidev/karte-ios-sdk>)のパッケージを追加してください。\
詳細は下記を参照してください。\
[Adding Package Dependencies to Your App | Apple Developer Documentation](https://developer.apple.com/documentation/swift_packages/adding_package_dependencies_to_your_app)

## リッチプッシュ通知の受信に必要な実装を行う

Extension 作成時にテンプレートから自動的に作成されるソースコードを修正します。\
デフォルトで作成される `NotificationService` クラスを `NotificationServiceExtension` クラスを継承するようにし、デフォルトの実装も削除します。

このようにすることで、KARTE 経由で配信されたプッシュ通知を受信した際に `NotificationServiceExtension` が自動的に必要なリソース（画像等）をダウンロードし、通知へ反映します。

{% tabs %}
{% tab title="Swift" %}
{% code title="NotificationService.swift" overflow="wrap" %}

```swift
import KarteNotificationServiceExtension

class NotificationService: NotificationServiceExtension {
}
```

{% endcode %}
{% endtab %}

{% tab title="Objective-C" %}
{% code title="NotificationService.m" overflow="wrap" %}

```objc
// Header
#import <UserNotifications/UserNotifications.h>
#import <KarteNotificationServiceExtension/KarteNotificationServiceExtension.h>

@interface NotificationService : KRTNotificationServiceExtension
@end

// Implementation
#import "NotificationService.h"

@interface NotificationService ()
@end

@implementation NotificationService
@end
```

{% endcode %}
{% endtab %}
{% endtabs %}

## KARTE 経由のプッシュ通知であるか判定する方法

プッシュ通知が KARTE 経由で送信されたものであるか判定する必要がある場合は、`NotificationServiceExtension` の `canHandleRemoteNotification` メソッドを呼び出します。

{% tabs %}
{% tab title="Swift" %}
{% code title="NotificationService.swift" overflow="wrap" %}

```swift
import KarteNotificationServiceExtension

class NotificationService: NotificationServiceExtension {

  override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
    // KARTE経由のプッシュ通知であるか判定
    if super.canHandleRemoteNotification(request.content.userInfo) {
      // KARTE経由のプッシュ通知 (スーパークラスに処理を移譲)
      super.didReceive(request, withContentHandler: contentHandler)
    } else {
      // KARTE以外のシステムから送信されたプッシュ通知 (任意の処理を実装)
      contentHandler(request.content)
    }
  }
}
```

{% endcode %}
{% endtab %}

{% tab title="Objective-C" %}
{% code title="NotificationService.m" overflow="wrap" %}

```objc
#import "NotificationService.h"

@interface NotificationService ()
@end

@implementation NotificationService

- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {
  // KARTE経由のプッシュ通知であるか判定
  if ([super canHandleRemoteNotification:request.content.userInfo]) {
    // KARTE経由のプッシュ通知 (スーパークラスに処理を移譲)
    [super didReceiveNotificationRequest:request withContentHandler:contentHandler];
  } else {
    // KARTE以外のシステムから送信されたプッシュ通知 (任意の処理を実装)
    contentHandler(request.content);
  }
}

@end
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# 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 dynamically by asking a question.

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

```
GET https://app.developers.karte.io/ios-sdk-appendix/appendix-rich-notification-ios-sdk.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
