# ディープリンク

PushSDK は、通知の開封を次の方法で処理します:

* デフォルトでは、SDK は自動的に次を呼び出します `open` メソッドを、通知に添付されたランディング URL を使用して呼び出します。
* 次を実装する場合 `didReceiveNotificationDestination` 通知が開かれたときに必要なカスタムロジックを実行できるライフサイクルデリゲートです。

## URL 処理デリゲートの実装

PushSDK は、購読者が通知とやり取りしたときに呼び出されるデリゲートを提供します。これらのデリゲートは、通知を開いた後に購読者が適切なコントローラー、ページ、またはその他のビューへ自動的に移動するよう実装する必要があります。

{% hint style="danger" %}
すべての SDK デリゲートは、アプリケーションの `AppDelegate` クラス内に配置して、すべての通知イベントを適切に処理することを推奨します。
{% endhint %}

SDK が通知のオープンイベントを受信すると、以下のいずれかのデリゲートを、アプリ内で購読者を適切なビューへ移動させるために使用できるパラメータ付きで呼び出そうとします。これらのデリゲートを実装するには、 `PNNotificationLifecycleDelegate`.

このプロトコルを実装するクラスを SDK に通知する必要があります。これは、 `setNotificationLifecycleDelegate` メソッドを SDK で使用して行えます:

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

```swift
PushSDK.setNotificationLifecycleDelegate(self)
```

{% endtab %}

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

```objectivec
[PushSDK setNotificationLifecycleDelegate:self];
```

{% endtab %}
{% endtabs %}

たとえば、通知内にランディング URL が提供されており、あなたの `AppDelegate` 内で購読者のナビゲーションを処理するデリゲートを実装している場合、コードは次のようになります：

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

```swift
import Pushly

@main
class AppDelegate: UIResponder, UIApplicationDelegate, PNNotificationLifecycleDelegate {
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        PushSDK.setConfiguration(appKey: "YOUR_APP_KEY", withLaunchOptions: launchOptions)
        PushSDK.PushNotifications.showPermissionPrompt() { granted, settings, error in
            // 任意のコールバック
            print("User accepted permissions: \(granted)")
        }
        PushSDK.setNotificationLifecycleDelegate(self)

        return true
    }

    func pushSDK(didReceiveNotificationDestination destination: String, withInteraction interaction: PNNotificationInteraction) -> Bool {
        // destination（ランディング URL）を使用して、アプリケーション内のビューへ移動
        
        // SDK に対して遷移しないよう通知するために true を返す
        return true
    }
}
```

{% endtab %}

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

```objectivec
@import Pushly;

@interface AppDelegate () <PNNotificationLifecycleDelegate>

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // アプリ起動後のカスタマイズ用のオーバーライドポイント。

    PushSDK.logLevel = PNLogLevelInfo;
    [PushSDK setConfigurationAppKey:@"YOUR_APP_KEY" withLaunchOptions:launchOptions];

    [PushSDKPushNotificationsSDK showPermissionPrompt:^(BOOL granted, UNNotificationSettings * _Nonnull settings, NSError * _Nullable error) {
        NSLog(@"User accepted permissions: %d", granted);
    }];
    
    [PushSDK setNotificationLifecycleDelegate:self];

    return YES;
}

- (BOOL)pushSDKDidReceiveNotificationDestination:(NSString *)destination withInteraction:(PNNotificationInteraction *)interaction {
    // destination（ランディング URL）を使用して、アプリケーション内のビューへ移動
    
    // SDK に対して、ナビゲートしないようにするには YES を返します
    return YES;
}
```

{% endtab %}
{% endtabs %}

## 通知インタラクション デリゲート

### URI/URL 宛先付きで通知が開かれた場合

このデリゲートは、ランディング URL が関連付けられた通知が開かれたときに呼び出されます。サブスクライバーは、その URL を表すビューへ移動する必要があります。

このメソッドは Boolean の応答を想定しています。応答が `true` の場合、この通知のオープンに対して SDK はナビゲーションを行いません。応答が `false` の場合、SDK は `open` メソッド内に追加します。

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

```swift
func pushSDK(didReceiveNotificationDestination destination: String, withInteraction interaction: PNNotificationInteraction) -> Bool {
    // destination（ランディング URL）を使用して、アプリケーション内のビューへ移動
        
    // SDK に対して遷移しないよう通知するために true を返す
    return true
}
```

{% endtab %}

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

```objectivec
- (BOOL)pushSDKDidReceiveNotificationDestination:(NSString *)destination withInteraction:(PNNotificationInteraction *)interaction {
    // destination（ランディング URL）を使用して、アプリケーション内のビューへリダイレクト
    
    // SDK に対して、リダイレクトしないことを通知するには YES を返します
    return YES;
}
```

{% endtab %}
{% endtabs %}

x
