2017-07-27 78 views
1

我在我的应用程序中安排了一批通知,其中一些带有重复间隔,其他的只是在特定日期发生的一次触发。我用这个方法来创建通知设置我的通知:UILocalNotification repeatInterval打破时区和fireDate

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

如果你比较'date'做了'Date.date()',是'date'在过去吗? – Larme

回答

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)