我有一個包含我的日期的數組。我想在上午6點30分安排那些日子的通知。使用Swift 3爲日期數組安排通知
我跟着appcoda tutorial它幫助安排通知從日期選擇器輸入很好,但我有點不確定如何調用我的函數安排通知只有給定的日子。
所以我的問題是如何以及在哪裏調用該函數?
- 的日子已經連續多日
- 我可以給該函數的開始日期,並在陣列中的項目數重複一次嗎?
下面是我的函數:
func scheduleNotification(at date: Date) {
let calendar = Calendar(identifier: .gregorian)
let components = calendar.dateComponents(in: .current, from: date)
let newComponents = DateComponents(calendar: calendar, timeZone: .current, month: components.month, day: components.day, hour: 6, minute: 30)
let trigger = UNCalendarNotificationTrigger(dateMatching: newComponents, repeats: false)
let content = UNMutableNotificationContent()
content.title = "Advent Calendar"
content.body = "Just a reminder to open your present!"
content.sound = UNNotificationSound.default()
let request = UNNotificationRequest(identifier: "textNotification", content: content, trigger: trigger)
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
UNUserNotificationCenter.current().add(request) {(error) in
if let error = error {
print("Uh oh! We had an error: \(error)")
}
}
}
請問這個新的FUNC scheduleNotification與你的問題之前的功能在您創建的通知互動?您的警報內容是否與所有警報 – tylerSF
@tylerSF相同,它會一次創建所有24個通知,計劃每天發送一次。該應用程序每天都會包含一項新禮物,因此通知相同,只是爲了讓用戶注意到這些信息。 –