Push Notification 사용 권한 요청 1. UNUserNotificationCenterDelegate 채택 2. Notification 권한 부여 요청 3. APNS(Apple Push Notification Service)에 등록
UNUserNotificationCenter란?
앱과 앱 익스텐션에 대한 알림 관련 작업을 관리하기 위한 센트럴 오브젝트. 공유된 UNUserNotificationCenter 객체를 사용하여 앱과 앱 확장의 모든 알림 관련 동작을 관리한다.
Push Notification 사용 권한 요청
알림에 응답하여 알럿을 표시하거나, 소리를 재생하거나, 앱 아이콘에 배지를 달 수 있는 권한을 요청하는 방법에 대해 알아보자.
1. UNUserNotificationCenterDelegate 채택
알림 관련 작업을 처리하려면 UNUserNotificationCenterDelegate 프로토콜을 채택하는 객체를 생성하여 이 객체의 델리게이트 프로퍼티에 할당한다. 이 작업은 해당 델리게이트와 상호작용을 할 수 있는 다른 작업을 수행 하기 전에 이 델리게이트 프로퍼티에 객체를 항상 할당하자.
승인을 요청하려면, UNUserNotificationCenter 인스턴스를 가져와서 requestAuthorization(options:completionHandler:) 을 호출하자. (alert, sound, badge 타입 요청 가능)
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
if let error = error {
// Handle the error here.
}
// Enable or disable features based on the authorization.
}
3. APNS(Apple Push Notification Service)에 원격 알림을 수신하도록 등록
Apple Push Notification Service에 등록 프로세스를 시작하려면 아래 메소드를 호출하자. (Main Thread에서 진행해야함.)