NowPushService class - iOS
The NowPushService class provides functions that enable interaction with the Push Notification service.
| 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.
| 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:
|
| completion | @escaping (Result<Data, NowPushError>) → Void | Completion handler to execute after the token has been attempted to be registered.
|
| 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.
| 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:
|
| completion | @escaping (Result<Data, NowPushError>) → Void | Completion handler to execute after the token has been attempted to be unregistered.
|
| 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.
| Name | Type | Description |
|---|---|---|
| _userInfo | [AnyHashable: Any] | Push payload content received as part of the push notification. |
| Type | Description |
|---|---|
| Result<NowPushPayload, NowPushError> | Returns the results of the payload parsing.
|
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(())
}