2016-09-10 95 views
0

我正在使用以下命令來獲取每小時觸發的本地通知。UNCalendarNotificationTrigger每小時觸發器返回錯誤的觸發日期

let reminderTrigger = UNCalendarNotificationTrigger.init(dateMatching: NSCalendar.current.dateComponents([.minute], from: dateStart), repeats: true) 
let notificationRequest = UNNotificationRequest(identifier: "reminder", content: reminderContent, trigger: reminderTrigger) 

這是基於Apple的文檔在這裏 - https://developer.apple.com/reference/usernotifications/uncalendarnotificationtrigger

然而,當我打印下一次觸發日期,這是完全錯誤的。

print(dateStart) 
print(reminderTrigger.nextTriggerDate()) 

這是輸出 -

2016-09-10 05:18:58 +0000 
Optional(2016-09-11 04:00:06 +0000) 

但是,它應該是

2016-09-10 06:00:00 +0000 

我試過每天,每週,每月,每年以類似的方式,所有的他們返回正確的下一個觸發日期小時是唯一看起來不正確的。有任何想法嗎?

+0

我有類似的問題,唯一的區別是唯一的偏移量是不正確的,即使在日期組件對象上設置'timeZone'。當日期到達時,通知不會被觸發,但它也未在待處理或已傳遞的通知中列出。 – fedoroffn

+0

你的TimeZone /日曆設置肯定有問題。如果您沒有指定小時,它會自動設置爲默認值,因此無論您的設備設置如何,它都能正常工作。 –

回答

0

我遇到了和你一樣的問題。

 UNNotificationRequest *request = notifyDict[[NSString stringWithFormat:@"%ld", indexPath.row]]; 
     UNTimeIntervalNotificationTrigger *trigger = (UNTimeIntervalNotificationTrigger *)request.trigger; 
     NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
     [dateFormatter setDateFormat:@"HH:mm:ss"]; 
     NSString *strDate = [dateFormatter stringFromDate:trigger.nextTriggerDate]; 

我得到觸發器的nextTriggerDate,但日期不是我爲觸發器設置的值。