2017-07-27 71 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) 
相關問題