Skip to content
Release: Australia · Updated: 2026-03-12 · Official documentation · View source

NowPushService class- iOS

The NowPushService class provides functions that enable interaction with the Push Notification service.

NameDescription
configurationConfiguration settings provided when the service was initialized.Data type: NowServiceConfiguration

Parent Topic:Mobile SDK - iOS

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.

NameTypeDescription
\_tokenDataData 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.
pushAppNameStringName of the push application for which this token is being registered.
environmentNowPushEnvironmentEnvironment for which the token is being registered.Valid values: - production - sandbox
completion@escaping \(Result​<Data, NowPushError>\) → VoidCompletion 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.
TypeDescription
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.

NameTypeDescription
\_tokenDataData object of the push token to unregister.
pushAppNameStringName of the push application for which this token is being unregistered.
environmentNowPushEnvironmentEnvironment for which the token is being registered.Valid values: - production - sandbox
completion@escaping \(Result​<Data, NowPushError>\) → VoidCompletion 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.
TypeDescription
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.

NameTypeDescription
_userInfo[AnyHashable: Any]Push payload content received as part of the push notification.
TypeDescription
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(())
}