> For the complete documentation index, see [llms.txt](https://documentation.pushly.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://documentation.pushly.com/pushly-ja/integration/implementation-steps/apple-ios/xiang-xi-she-ding/serufumanjido.md).

# セルフマネージド統合

統合を容易にするため、PushSDK は method swizzling を使用して、アプリケーションデリゲートと user notification center に自動的に組み込まれます。次のような状況では、自動統合を無効にしたい場合があります。

* swizzling やその他の自動統合を含む他の SDK との競合
* 競合するサードパーティ製の開発ソリューション
* 当社の method swizzling が、既存のアーキテクチャと競合する

{% hint style="info" %}
swizzling が既存のコードと互換性がない場合にのみ、自前管理の統合を検討することをおすすめします。自前管理の統合を使用する場合、PushSDK の今後のリリースで、手動で呼び出す必要がある追加メソッドが増えないように注意する必要があります。
{% endhint %}

次のメソッド呼び出しを追加してください **前に** 呼び出す `PushSDK.setConfiguration`:

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

```swift
PushSDK.disableMethodSwizzling()
```

{% endtab %}

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

```objectivec
[PushSDK disableMethodSwizzling];
```

{% endtab %}
{% endtabs %}

{% hint style="warning" %}
これは **必ず** 次を呼び出す前に実行する必要があります\
`PushSDK.setConfiguration(appKey: myAppKey, withLaunchOptions: launchOptions)`
{% endhint %}

### 順序が重要な理由

`disableMethodSwizzling()` フックが **インストールされるのを防ぐだけです**。インストールは `setConfiguration`の実行中に行われるため、その後で呼び出しても何の意味もありません:

* フックはすでに有効になっており、すべての通知コールバックを引き続き転送します。
* SDK は `UNUserNotificationCenter` の delegate スロットをインストール時に取得し、 `UNUserNotificationCenter` そのスロットを返す方法はありません。したがって、SDK はプロセスの寿命が尽きるまで通知デリゲートのままになります。

自前管理の統合が swizzling がまだ有効であるかのように動作する場合は、デバッグ時に、 `setConfiguration` あなたの `disableMethodSwizzling()` 呼び出しより前に何も実行されていないことを確認してください。— SDK を別のエントリポイントから構成するコードパスも含みます。

### 自動統合と手動統合は絶対に併用しないでください

{% hint style="danger" %}
自動統合と自前管理の統合は相互排他的です。フックがインストールされていて、アプリが **さらに** 以下の手動エントリポイントも呼び出すと、すべての通知が **2 回** 処理されます — インストール済みのフックとあなた自身の呼び出しの両方が同じコードパスを実行し、重複したインプレッションとイベントが発生します。
{% endhint %}

二重処理は、デバイスログでは重複した completionHandler 呼び出しとして表示されます:

```
[PNUserNotificationCenter] willPresent completionHandler は既に呼び出されています — 重複した呼び出しを無視しています
[PNUserNotificationCenter] didReceive completionHandler は既に呼び出されています — 重複した呼び出しを無視しています
[PNApplication] completionHandler は既に呼び出されています — 重複した呼び出しを無視しています
```

1 つの統合スタイルを選択し、一貫して使用してください:

* **自動（デフォルト）** — 以下に記載された手動メソッドは呼び出さないでください。
* **自前管理** — を呼び出し、 `disableMethodSwizzling()` 前に `setConfiguration`、その後、以下に記載されたすべての手動メソッドを実装してください。

swizzling を無効にすると、swizzling されなくなったメソッドを呼び出すために PushSDK への呼び出しも追加で必要になります。以下のメソッドをすべて実装しない場合、PushSDK が正しく動作しない可能性があります。

手動で呼び出す必要があるメソッドは次のとおりです:

* `application:didRegisterForRemoteNotificationsWithDeviceToken:`
* `application:didFailToRegisterForRemoteNotificationsWithError:`
* `application:didReceiveRemoteNotification:fetchCompletionHandler:`
* `userNotificationCenter:willPresent:withCompletionHandler:`
* `userNotificationCenter:didReceive:withCompletionHandler:`
* iOS 10 以前を対象にしている場合:
  * `application:didReceiveRemoteNotification:`

### UIApplicationDelegate の例

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

```swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    PushSDK.disableMethodSwizzling()

    PushSDK.setConfiguration(appKey: "REPLACE_WITH_SDK_KEY", withLaunchOptions: launchOptions)

    PushSDK.showNativeNotificationPermissionPrompt() { granted, settings, error in
        // 任意のコールバック
        print("ユーザーが権限を承認しました: \\(granted)")
    }

    return true
}

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    PushSDK.application(application, didRegisterForRemoteNotificationsWithDeviceToken: deviceToken)
}

func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    PushSDK.application(application, didFailToRegisterForRemoteNotificationsWithError: error)
}

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    PushSDK.application(application, didReceiveRemoteNotification: userInfo) { result in
        // PushSDK の UIBackgroundFetchResult を受け取り、必要に応じて独自のロジックに合わせて変更してください
        completionHandler(result)
    }
}

// iOS バージョン 10 以前を対象とする統合の場合
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
    PushSDK.application(application, didReceiveRemoteNotification: userInfo)
}
```

{% endtab %}

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

```objectivec
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [PushSDK disableMethodSwizzling];

    [PushSDK setConfigurationAppKey:@"REPLACE_WITH_SDK_KEY" withLaunchOptions:launchOptions];

    [PushSDK showNativeNotificationPermissionPrompt:^(BOOL granted, UNNotificationSettings * _Nonnull settings, NSError * _Nullable error) {
        NSLog(@"ユーザーが権限を承認しました: %@", granted ? @"YES" : @"NO");
    }];

    return YES;
}

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
{
    [PushSDK application:application didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error
{
    [PushSDK application:application didFailToRegisterForRemoteNotificationsWithError: error];
}

- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
{
    [PushSDK application:application didReceiveRemoteNotification:userInfo fetchCompletionHandler:^(UIBackgroundFetchResult result) {
        // PushSDK の UIBackgroundFetchResult を受け取り、必要に応じて独自のロジックに合わせて変更してください
        completionHandler(result);
    }];
}

// iOS バージョン 10 以前を対象とする統合の場合
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
    [PushSDK application:application didReceiveRemoteNotification:userInfo];
}
```

{% endtab %}
{% endtabs %}

### UNUserNotificationCenterDelegate の例

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

```swift
public func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    PushSDK.userNotificationCenter(center, willPresent: notification) { options in
        // PushSDK の UNNotificationPresentationOptions を受け取り、必要に応じて独自のロジックに合わせて変更してください
        completionHandler(options)
    }

}

public func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    PushSDK.userNotificationCenter(center, didReceive: response) {
        completionHandler()
    }
}
```

{% endtab %}

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

```objectivec
- (void)userNotificationCenter:(UNUserNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions))completionHandler
{
    [PushSDK userNotificationCenter:center willPresent:notification withCompletionHandler:^(UNNotificationPresentationOptions options) {
        // PushSDK の UNNotificationPresentationOptions を受け取り、必要に応じて独自のロジックに合わせて変更してください
        completionHandler(options);
    }];    
}

- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{
    [PushSDK userNotificationCenter:center didReceive:response withCompletionHandler:^{
        completionHandler();
    }];
}
```

{% 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://documentation.pushly.com/pushly-ja/integration/implementation-steps/apple-ios/xiang-xi-she-ding/serufumanjido.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.
