NowPushService class - iOS

  • リリースバージョン: Australia
  • 更新日 2026年03月12日
  • 所要時間:9分
  • The NowPushService class provides functions that enable interaction with the Push Notification service.

    表 : 1. Properties
    Name Description
    configuration Configuration settings provided when the service was initialized.

    Data type: NowServiceConfiguration

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

    Registers a unique token with the ServiceNow instance used to identify the push notifications for the current iOS device and specified application.

    表 : 2. Parameters
    Name Type Description
    _token Data Data object of the push token to register. This is the token obtained from the Apple Developer site. For more information, see the Mobile SDK Developer Guide - iOS.
    pushAppName String Name of the push application for which this token is being registered.
    environment NowPushEnvironment Environment for which the token is being registered.
    Valid values:
    • production
    • sandbox
    completion @escaping (Result​<Data, NowPushError>) → Void Completion handler to execute after the token has been attempted to be registered.
    • Success: iOS push notification certificate.
    • Error: NowPushError - Information about the error that occurred when trying to register the token.
      Possible values:
      • decoding: Decoding error occurred.
      • httpRequestFailure: Generic error that is returned whenever the http call is not a success (2xx).
      • notAuthorized: Requestor is not authorized to perform the specified push notification operation.
      • unsupportedData: Push payload is invalid.
    表 : 3. Returns
    Type Description
    None

    This example shows how to register a push notification token for the TestPushApp application.

    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: String, environment: NowPushEnvironment, completion: @escaping (Result<Data, NowPushError>) -> Void)

    Unregisters a previously registered push token.

    表 : 4. Parameters
    Name Type Description
    _token Data Data object of the push token to unregister.
    pushAppName String Name of the push application for which this token is being unregistered.
    environment NowPushEnvironment Environment for which the token is being registered.
    Valid values:
    • production
    • sandbox
    completion @escaping (Result​<Data, NowPushError>) → Void Completion handler to execute after the token has been attempted to be unregistered.
    • Success: API response data.
    • Error: NowPushError - Information about the error that occurred when trying to unregister the token.
      Possible values:
      • decoding: Decoding error occurred.
      • httpRequestFailure: Generic error that is returned whenever the http call is not a success (2xx).
      • notAuthorized: Requestor is not authorized to perform the specified push notification operation.
      • unsupportedData: Push payload is invalid.
    表 : 5. Returns
    Type Description
    None

    This example shows how to unregister the push notification token associated with the TestPushApp application.

    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])

    Parses the userInfo key-value pairs into a NowPushPayload object.

    表 : 6. Parameters
    Name Type Description
    _userInfo [AnyHashable: Any] Push payload content received as part of the push notification.
    表 : 7. Returns
    Type Description
    Result<NowPushPayload, NowPushError> Returns the results of the payload parsing.
    • Success: Parsed NowPushPayload object.
    • Failure: NowPushError - Information about the error that was encountered while parsing the specified user payload.
      Possible values:
      • decoding: Decoding error occurred.
      • httpRequestFailure: Generic error that is returned whenever the http call is not a success (2xx).
      • notAuthorized: Requestor is not authorized to perform the specified push notification operation.
      • unsupportedData: Push payload is invalid.

    This example shows how to parser a user payload.

    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(())
    }