我在我的应用中安排了一批通知,其中一些是重复间隔,另一些则是在特定日期点火。我使用此方法设置通知以创建通知:
func notification(date: Date, repeatUnit: NSCalendar.Unit?) -> UILocalNotification {
let notification = UILocalNotification()
notification.category = "ReminderCategory"
notification.alertTitle = "Test"
notification.alertBody = "Test Body"
notification.soundName = "Sound1.m4a"
notification.fireDate = date
notification.repeatInterval = repeatUnit ?? NSCalendar.Unit(rawValue: 0)
notification.timeZone = TimeZone.init(secondsFromGMT: 0)!
return notification
}
如果repeatUnit
变量设置为NSCalendar.Unit
单位中的任何一个,则会在正确的时间(对于本地时区)触发通知。
但是,如果我没有设置repeatInterval
,过去会以某种方式设置通知fireDates,并在我安排通知后立即触发。
有没有人知道发生了什么?
答案 0 :(得分:0)
这适合我。
let center = UNUserNotificationCenter.current()
let content = UNMutableNotificationContent()
content.title = "Title"
content.body = "Your text"
content.categoryIdentifier = "reminder-notification"
content.sound = UNNotificationSound.default()
var dateComponents = DateComponents()
dateComponents.hour = Calendar.current.component(.hour, from: date)
dateComponents.minute = Calendar.current.component(.minute, from: date)
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
center.add(request)