[회고] 신입 iOS 개발자가 되기까지 feat. 카카오 자세히보기

🍎 Apple/Swift

[Swift] UNUserNotificationCenter 살펴보기

inu 2022. 5. 26. 14:04
반응형

UNUserNotificationCenter

UNUserNotificationCenter

  • UNUserNotificationCenter는 앱 혹은 앱 확장에서 발생하는 알림 관련 활동들을 관리할 수 있는 중앙 객체입니다.

  • 앱에서 관련 기능을 다루기 위해서는 먼저 권한을 요청해야 합니다.
  • options에 필요한 권한을 포함시켜 전달하고, 그 결과를 completionHandler로 받아 추가 작업을 수행합니다.

  • options에 포함시킬 수 있는 권한은 위와 같습니다.

  • 싱글톤 객체를 제공하기 때문에 이를 활용하면 됩니다.

  • 여기에 아래에서 학습할 UNNotificationRequest 객체를 추가해서 알림을 설정합니다.

UNMutableNotificationContent

  • 알림의 내용을 설정할 수 있는 객체입니다.

  • title, subtitle, body, badge, sound 등을 설정할 수 있습니다.
  • cf. badge는 앱아이콘 오른쪽위에 표기되는 알람 개수를 의미합니다.

UNNotificationTrigger

  • 알림을 트리거하는 조건에 대한 패턴을 정의할 수 있는 추상 클래스입니다.

  • 이에 대한 구체타입으로는 아래 4가지가 존재합니다.
    • UNTimeIntervalNotificationTrigger
    • UNCalendarNotificationTrigger
    • UNLocationNotificationTrigger
    • UNPushNotificationTrigger

UNTimeIntervalNotificationTrigger

// Fire in 30 minutes (60 seconds times 30)
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: (30*60), repeats: false)
  • 설정한 timeInterval 값이 지나면 알림을 트리거합니다.

UNCalendarNotificationTrigger

var date = DateComponents()
date.hour = 8
date.minute = 30 
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
  • 특정 날짜 혹은 시간에 알림을 트리거합니다.
  • 내부 값으로는 DateComponet 객체를 활용합니다.

UNLocationNotificationTrigger

let center = CLLocationCoordinate2D(latitude: 37.335400, longitude: -122.009201)
let region = CLCircularRegion(center: center, radius: 2000.0, identifier: "Headquarters")
region.notifyOnEntry = true
region.notifyOnExit = false
let trigger = UNLocationNotificationTrigger(region: region, repeats: false)
  • 특정 지역에 도착했을 때 알림을 트리거합니다.

UNPushNotificationTrigger

  • Apple Push Notification Service (APNs)에서 알림을 보냈을 때 알림을 트리거합니다.
  • 사용자가 직접 해당 클래스를 생성하여 사용할 일은 없습니다.

UNNotificationRequest

  • 알림을 요청하는 핵심객체입니다. 내부에 앞서 학습한 UNMutableNotificationContent 및 UNNotificationTrigger가 포함됩니다.

  • identifier도 포함되기 때문에 추후 UNUserNotificationCenter에서 이를 기반으로 알림 삭제도 가능합니다.
반응형

'🍎 Apple > Swift' 카테고리의 다른 글

[Swift] Mirror  (0) 2023.05.18
[Swift] autoreleasepool  (2) 2022.05.27
[Swift] PhotoKit으로 사진앱에서 사진 가져오기  (0) 2022.05.22
[Swift] URLSession Cahce Policy  (1) 2022.05.18
[Swift] URL Loading System  (0) 2022.05.12