# セルフマネージド統合

統合をより簡単にするため、PushSDK はメソッドスワイジングを使用して、アプリケーションデリゲートとユーザ通知センターに自動的に統合されます。次のような複数のシナリオでは、自動統合を無効にしたい場合があります:

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

{% hint style="info" %}
スワイジングが既存のコードと互換性がない場合にのみ、自己管理型の統合を検討することをお勧めします。自己管理型の統合を使用する場合は、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 %}

スワイジングを無効にすると、もはやスワイジングされないメソッドを呼び出すために、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("User accepted permissions: \(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 %}
