iOS 10+ 使用 UserNotifications (UN) 框架,替代舊 UILocalNotification。
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
if (granted) { print("授權成功") }
}
let content = UNMutableNotificationContent()
content.title = "鬧鐘"
content.body = "起床囉!"
content.sound = .default
content.badge = 1
var date = DateComponents()
date.hour = 7
date.minute = 30
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
let request = UNNotificationRequest(identifier: "alarm_0730", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(request) { error in ... }
UNUserNotificationCenterDelegate,willPresent 回傳 [.alert, .sound]。.caf/.wav/.mp3 放入 Bundle,content.sound = UNNotificationSound(named: "custom.caf")。UNNotificationAction + UNNotificationCategory,支援「稍後提醒」、「關閉」等互動。UNNotificationServiceExtension 修改推播內容、下載圖片。新浪部落格完整教學:iOS 鬧鐘程式實作
提供上述部落格連結。