- 개요
- 노티피케이션은 푸시 방식
- 종류
- 디바이스에서 발생시키는 로컬 노티피케이션
- 서버에서 보내주는 리모트 노티피케이션
- 컴포넌트
- User-facing: 유저에게 드러나는 부분
- Silent
- content-available 플래그를 넣으면 백그라운드에서 앱을 리프레시할 수 있음
- 기존 API
- UIApplication을 통해서
- 로컬과 리모트용 콜백이 분리
- 한번 스케쥴 되면 이를 수정하기 어려움
- 멀티 플랫폼 대응이 어려움
- User Notification 프레임워크
- 위 단점들을 수정한 것
- OS별로의 의의
- watchOS에서 로컬 노티 가능
- tvOS에서의 뱃지 기능 활용 가능
- 사용방법
UNUserNotificationCenter.current().requestAuthorization([.alert, .sound, .badge]) { (granted, error) in //... }
// 유저의 설정값을 가져옴
UNUserNotificationCenter.current().getNotificationSettings { (settings) in //... }
// 기존 API
// APNS와 통신해서 디바이스 토큰을 얻어오기 때문에 네트워크가 필수적으로 필요
// 이렇게 받은 정보는 서버로 올려야 된다 -> 푸시 페이로드에 저 디바이스 토큰이 필요하기 때문에
UIApplication.shared().registerForRemoteNotifications()
// 로컬용
let content = UNMutableNotificationContent()
content.title = "Introduction to Notifications"
content.subtitle = "Session 707"
content.body = "Woah! These new notifications look amazing! Don't you agree?"
content.badge = 1
// 리모트 용
{
"aps" : {
"alert" : {
"title": "Introduction to Notifications",
"subtitle": "Session 707",
"body": "Woah! These new notifications look amazing! Don't you agree?"
},
"badge": 1
}
}
// 시작 기준은 항상 현재
UNTimeIntervalNotificationTrigger(timeInterval: 120, repeats: false)
UNTimeIntervalNotificationTrigger(timeInterval: 3600, repeats: true)
let dateComponents = DateComponents()
UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let region = CLRegion()
// Region 설정
UNLocationNotificationTrigger(region: region, repeats: false)
- 스케쥴링
- 로컬: UNUserNotificationCenter에 request를 add한다.
- 리모트: 서버에서 APNS를 통해서 OS에 전달한다.
import UserNotification
UNUserNotificationCenter.current().requestAuthorization([.alert, .sound, .badge]) { granted. error in //... }
let content = UNMutableNotificationContent()
content.title = "..."
content.body = "..."
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
let requestIdentifier = "..."
let request = UNNotificationRequest(identifier: requestIdentifier,
content: content,
trigger: trigger)
UNUserNotificationCenter.current().add(request) { (error) in //... }
protocol UNUserNotificationCenterDelegate: NSObjectProtocol
// foreground일 때 처리
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notificaiton: UNUserNotification,
withCompletionHandler completionHandler: (UNNotificationPrensentationOptions) -> Void)
- 노티피케이션 관리
- 이번 프레임워크와 동시에 해금된 기능
- 발생전이거나(Pending, 로컬), 이미 발생했던(로컬, 리모트) 노티피케이션에 접근 가능
- 이미 발생된 노티피케이션 제거 및 업데이트 가능
- 노티피케이션 identifier를 기준으로 찾는다.
- 로컬은 리퀘스트 만들 때 줬던 id
- 리모트는 APNS-collapse-id 필드 참조