Grouped notifications
기본적으로 자동으로 그룹화
커스텀 그루핑을 위해서는 Thread Identifier를 사용(기존에 있던 프로퍼티)
let content = UNMutableNotificationContent()
content.title = "New Photo"
content.body = "Jane Doe Posted a new Photo"
content.threadIdentifier = "thread-identifier"
// remote notification
{
"aps" : {
"alert" : {
"title" : "New Photo",
"body" : "Jane Doe posted a new photo",
"thread-id": "thread-identifier",
},
},
}
동작 원리
사용자 옵션
Notification content extensions - iOS 10에 이미 도입되었던 것
현재는 acion이 dynamic하지 않고, 카테고리에 묶여있다는 문제가 있다.
그래서 NSExtensionContext에 notificationActions를 정의하게 되었다.
extension NSExtensionContext {
@avaliable(iOS 12.0, *)
var notificationActions: [UNNotificationAction]
}
class NotificationViewController: UIViewController, UNNotificationContentExtension {
func didReceive(_ response: UNNotificationResponse, completionHandler completion: (UNNotificationContentExtensionResponseOption) -> Void) {
if response.actionIdentifier == "like-action" {
// update state...
let unlikeAction = UNNotificationAction(identifier: "unlike-action",
title: "Unlike", options: [])
let currentActions = extensionContext?.notificationActions
let commentAction = currentActions![1]
let newActions = [ unlikeAction, commentAction ]
extensionContext?.notificationActions = newActions
}
}
}
이제 action을 바꾸기 위해서 카테고리를 아예 바꿀 필요가 없다. 뷰 셋업할 때 아예 할당해놓는 것도 된다.
func didReceive(_ notification: UNNotification) {
// Set up Content extension view...
extensionContext?.notificationActions = newActions
}
계층적으로 동작하는 것처럼 보이게도 할 수 있다.
func didReceive(_ response: UNNotificationResponse,
completionHandler completion: (UNNotificationContentExtensionResponseOption) -> Void) {
if response.actionIdentifier = "rate-action" {
extensionContext?.notificationActions = [ oneStarAction, twoStarAction, threeStarAction ]
}
}
아예 액션을 지울 수도 있다.
func didReceive(_ response: UNNotificationResponse,
completionHandler completion: (UNNotificationContentExtensionResponseOption) -> Void) {
if response.actionIdentifier = "rate-action" {
extensionContext?.notificationActions = [ ]
}
}
iOS 12부터 Notification Content에서 유저 인터렉션이 허용된다.
앱 실행
기존 앱 실행 경로
커스텀 컨트롤로 실행하기 위해, 새로운 API 추가
extension NSExtensionContext {
@available(iOS 12.0, *)
func performNotificationDefaultAction()
}
커스텀 컨트롤로 notification dismiss하기
extension NSExtensionContext {
@available(iOS 12.0, *)
func dismissNotificationContentExtension()
}
Notification management
설정에 들어가지 않고도, Notification을 swipe하면 manage가 가능하도록 변경
Delivery 옵션
기본 값에 더해, 앱 내에서 커스텀할 수 있도록 엔트리포인트를 제공할 수 있다.
class AppDelegate: UIApplicationDelegate, UNUserNotificationCenterDelegate {
func userNotificationCenter(_ center: UNUserNotificationCenter,
openSettingsFor notification: UNNotification? ) {
// notification을 통해서 들어왔으면 notification이 있을 것이고
// 설정 화면을 통해서 들어왔으면 notifiacation이 nil일 것이다.
}
}
Provisional authorization
notificationCenter.requestAuthorization(
options: [.badge, .sound, .alert, .provisional]) {
}
Critical alerts
notificationCenter.requestAuthorization(
options: [.badge, .sound, .alert, .criticalAlert]) {
}
// 소리 설정
let content = UNMutableNotificationContent()
content.title = "WARNING: LOW BLOOD SUGAR"
content.body = "Glucose level at 57"
content.categoryIdentifier = "low-glucose-alert"
content.sound = UNNotificationSound.defaultCritical
// 커스텀 사운드 설정
content.sound = .criticalSoundNamed(.init("warning-sound"), withAudioVolume: 1.00)
// push payload
{
"aps" : {
"sound": {
"critical" : 1,
"name": "warning-sound.aiff",
"volume": 1.0
}
}
}