애플리케이션에서 푸시 알림 iOS 설정
푸시 알림을 구성하기 위해 인스턴스에서 수행해야 ServiceNow 하는 작업 외에 애플리케이션에 특정 코드도 포함해야 합니다 iOS .
NowSDK NowPushService 생성
애플리케이션에서 가장 먼저 수행해야 할 작업 중 하나는 NowPushService를 만드는 것입니다. NowPush SDK는 이 서비스를 생성하기 위한 팩토리 함수를 제공합니다. 기본 코드 본문 앞부분에 다음 코드 조각과 유사한 코드를 추가합니다.
func setup(with instanceURL: URL) -> AnyPublisher<NowService, ConfigurationError> {
NowPush.makePushService(instanceUrl: instanceURL)
.mapError { .sdkError($0) }
.map { $0 as NowService }
.eraseToAnyPublisher()
}
NowPush.makePushService() 메서드에 대한 자세한 내용은 을 참조하십시오NowPush API - iOS.
푸시 토큰 등록
Apple 는 푸시 알림을 수신할 장치와 애플리케이션을 식별하는 고유한 푸시 알림 토큰을 제공합니다. 애플리케이션이 푸시 알림을 수신하기 위해서는 NowPushService를 사용하여 이 토큰을 등록해야 합니다. 응용 프로그램에서 다음 코드 조각 iOS 과 유사한 함수를 추가합니다. 이 애플리케이션은 인스턴스에 등록되어 있어야 합니다 ServiceNow .
func registerForPushNotifications(deviceToken: Data) {
pushService.registerPushToken(deviceToken,
pushAppName: "TestPushApp", // Modify this to be your iOS application name
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
}
}
}
registerPushToken() 메서드에 대한 자세한 내용은 을 참조하십시오NowPushService - registerPushToken(_token: Data, pushAppName: String, environment: NowPushEnvironment, completion: @escaping (Result<Data, NowPushError>) -> Void).
푸시 토큰 등록 취소
사용자가 애플리케이션을 종료할 때마다(예: 사용자가 로그아웃할 때) 푸시 토큰을 등록 취소해야 합니다. 다음 코드 조각과 유사한 코드를 사용하여 푸시 토큰의 등록을 취소합니다. 애플리케이션의 이름으로 변경해야 pushAppName 합니다 iOS .
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
}
}
}
unregisterPushToken() 메서드에 대한 자세한 내용은 을 참조하십시오NowPushService - unregisterPushToken(_token: Data, pushAppName: String, environment: NowPushEnvironment, completion: @escaping (Result<Data, NowPushError>) -> Void).
푸시 페이로드 구문 분석
정보를 수집하고 애플리케이션의 다른 부분에서 데이터를 사용하려면 푸시 알림 페이로드를 NowPushPayload 객체로 구문 분석해야 합니다. 현재 이 역할은 Virtual Agent 푸시 알림에만 사용할 수 있습니다. 다음과 유사한 코드를 사용합니다
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(())
}