NowPushService クラス: iOS

  • リリースバージョン: Washingtondc
  • 更新日 2024年02月01日
  • 読む7読むのに数分
  • クラスには、プッシュ通知サービスとの対話を有効にする関数が用意されています。

    表 : 1. プロパティ
    名前 説明
    構成 サービスの初期化時に提供された構成設定。

    データタイプ: NowServiceConfiguration

    NowPushService - registerPushToken(_token: Data, pushAppName: 文字列, environment: NowPushEnvironment, completion: @escaping (Result<Data, NowPushError>) -> Void)

    現在のiOSデバイスと指定されたアプリケーションのプッシュ通知を識別するために使用されるインスタンスに一意のトークンServiceNowを登録します。

    表 : 2. パラメーター
    名前 タイプ 説明
    _トークン データ 登録するプッシュトークンのデータオブジェクト。これは、開発者サイトから取得した Apple トークンです。詳細については、『開発者ガイドiOS』を参照してくださいMobile SDK
    pushAppName 文字列 このトークンが登録されているプッシュ アプリケーションの名前。
    環境 NowPushEnvironment トークンが登録される環境。
    有効な値:
    • production
    • サンドボックス
    完了 @escaping (result<data, nowPushError>) → void トークンの登録が試行された後に実行する完了ハンドラー。
    • 成功:iOS プッシュ通知証明書。
    • エラー:NowPushError - トークンを登録しようとしたときに発生したエラーに関する情報。
      可能な値:
      • デコード: デコード エラーが発生しました。
      • httpRequestFailure:HTTP 呼び出しが成功しなかったときに返される一般的なエラー (2xx)
      • notAuthorized:要求者は指定されたプッシュ通知操作を実行する権限がありません。
      • unsupportedData:プッシュペイロードが無効です。
    表 : 3. 返される内容
    タイプ 説明
    なし

    この例では、TestPushApp アプリケーションのプッシュ通知トークンを登録する方法を示します。

    func registerForPushNotifications(deviceToken: Data) {
      pushService.registerPushToken(deviceToken,
          pushAppName: "TestPushApp",
          environment: environment) { [weak self] result in
        guard let self = self else { return }
        switch result {
          case .success:
            UserDefaults.standard.set(true, forKey: "pushNotificationsRegistered")
            self.pushRegistrationState = .registrationSuccess
          case .failure:
            UserDefaults.standard.set(false, forKey: "pushNotificationsRegistered")
            self.pushRegistrationState = .registrationFailed
        }
      }
    }

    NowPushService - unregisterPushToken(_token: Data, pushAppName: 文字列, environment: NowPushEnvironment, completion: @escaping (Result<Data, NowPushError>) -> Void)

    以前に登録したプッシュトークンの登録を解除します。

    表 : 4. パラメーター
    名前 タイプ 説明
    _トークン データ 登録解除するプッシュトークンのデータオブジェクト。
    pushAppName 文字列 このトークンが登録解除されるプッシュ アプリケーションの名前。
    環境 NowPushEnvironment トークンが登録される環境。
    有効な値:
    • production
    • サンドボックス
    完了 @escaping (result<data, nowPushError>) → void トークンの登録解除が試行された後に実行する完了ハンドラー。
    • 成功: API 応答データ。
    • エラー: NowPushError - トークンの登録を解除しようとしたときに発生したエラーに関する情報。
      可能な値:
      • デコード: デコード エラーが発生しました。
      • httpRequestFailure:HTTP 呼び出しが成功しなかったときに返される一般的なエラー (2xx)
      • notAuthorized:要求者は指定されたプッシュ通知操作を実行する権限がありません。
      • unsupportedData:プッシュペイロードが無効です。
    表 : 5. 返される内容
    タイプ 説明
    なし

    この例では、TestPushApp アプリケーションに関連付けられているプッシュ通知トークンの登録を解除する方法を示します。

    func unregisterFromPushNotifications(deviceToken: Data) {
      pushService.unregisterPushToken(deviceToken, pushAppName: "TestPushApp", environment: environment) { [weak self] result in
        guard let self = self else { return }
        switch result {
          case .success:
            UserDefaults.standard.set(false, forKey: "pushNotificationsRegistered")
            self.pushRegistrationState = .unregisterSuccess
          case .failure:
            self.pushRegistrationState = .unregisterFailed
        }
      }
    }

    NowPushService - payloadFromUserInfo(_userInfo: [AnyHashable: Any])

    userInfo のキーと値のペアを解析して NowPushPayload オブジェクトにします。

    表 : 6. パラメーター
    名前 タイプ 説明
    _Userinfo [AnyHashable: Any] プッシュ通知の一部として受信したプッシュ ペイロード コンテンツ。
    表 : 7. 返される内容
    タイプ 説明
    Result<NowPushPayload, NowPushError> ペイロード解析の結果を返します。
    • 成功: 解析済みの NowPushPayload オブジェクト。
    • エラー: NowPushError - 指定されたユーザーペイロードの解析中に発生したエラーに関する情報。
      可能な値:
      • デコード: デコード エラーが発生しました。
      • httpRequestFailure:HTTP 呼び出しが成功しなかったときに返される一般的なエラー (2xx)
      • notAuthorized:要求者は指定されたプッシュ通知操作を実行する権限がありません。
      • unsupportedData:プッシュペイロードが無効です。

    この例は、ユーザーペイロードをパーサーする方法を示しています。

    func userNotificationCenter(_ center: UNUserNotificationCenter,
        didReceive response: UNNotificationResponse,
        withCompletionHandler completionHandler: @escaping () -> Void) {
      guard let pushService = pushService else {
        completionHandler()
        return
      }
      let userInfo = response.notification.request.content.userInfo
      _ = pushService.payloadFromUserInfo(userInfo)
        .flatMap(handleNowPushPayload)
    }
    
    
    func handleNowPushPayload(_ payload: NowPushPayload) -> Result<Void, NowPushError> {
      guard payload is NowPushVirtualAgent else {
        return .failure(NowPushError.unsupportedData)
      }
      launchVirtualAgent()
      return .success(())
    }