User Authorization ==> Notification Request ==> Notify// AppDelegate
class AppDelegate {
func application(.., didFinishLaunchingWithOptions) {
UNUserNotificationCenter.current().delegate = self
getAuthorization() // 런칭 시점에 권한을 획득하는 알럿을 띄우자.
}
}
extension AppDelegate: UNUserNotificationCenterDelegate {
func getAuthorization() {
UNUserNotificationCenter.current().requestAuthorization(
options: UNAuthorizationOptions(arrayLiteral: .alert, .badge, .sound))
{ success, fail in
if success {
print("success")
} else {
if let fail {
print(fail)
}
}
}
}
}// 간단하게 알림을 관리하는 구조체를 작성해봤다.
struct ControlNotification {
enum NotificationInterval: Double {
case byFiveSeconds = 5
case byOneMinute = 60
case byOneHour = 3600
case byHalfSingleDay = 43200
case bySingleDay = 86400
}
static func sendNotification(_ t: String, body: String, seconds: NotificationInterval, repeats: Bool = false) {
let content = UNMutableNotificationContent()
content.title = t
content.body = body
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: seconds.rawValue, repeats: repeats)
let request = UNNotificationRequest(
identifier: "inapp-repeat-noti-1",
content: content,
trigger: trigger
)
UNUserNotificationCenter.current().add(request)
}
}