为一组日期安排 Swift 3 的通知
Scheduling notification with Swift 3 for an array of dates
我有一个包含我的日期的数组。我想在早上 6.30 安排那些日子的通知。
我遵循了 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)")
}
}
}
是的,所以在咨询开发人员后 @gklka 我决定使用一个简单的 for 循环,重复 24 次)并将索引传递给函数的日期 属性,在那里我预先配置小时、分钟、年和月,如下所示:
func scheduleNotification(day: Int) {
var date = DateComponents()
date.year = 2016
date.month = 11
date.day = day
date.hour = 6
date.minute = 30
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: false)
}
和 for 循环:
for index in 1...24 {
scheduleNotification(day: index)
}
因为我在 int AppDelegate 中设置了所有内容,所以我调用了 didFinishLaunchingWithOptions
中的函数
12月1日更新
所以我让一切保持原样,但早上没有收到通知。 .我查看了我的代码以找出原因。有 2 个问题。
我的函数中有一行代码会在遍历我的循环时删除之前设置的任何通知,所以我将下面的代码行注释掉了,但仍然无法正常工作正如预期的那样。 UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
我发现我已经用相同的 requestIdentifier 安排了我的通知,这基本上让我在最后一天只有 1 个通知。我只是通过字符串插值在自定义 ID 变量的末尾添加了索引,如下所示:let requestId = "textNotification\(day)"
我有一个包含我的日期的数组。我想在早上 6.30 安排那些日子的通知。
我遵循了 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)")
}
}
}
是的,所以在咨询开发人员后 @gklka 我决定使用一个简单的 for 循环,重复 24 次)并将索引传递给函数的日期 属性,在那里我预先配置小时、分钟、年和月,如下所示:
func scheduleNotification(day: Int) {
var date = DateComponents()
date.year = 2016
date.month = 11
date.day = day
date.hour = 6
date.minute = 30
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: false)
}
和 for 循环:
for index in 1...24 {
scheduleNotification(day: index)
}
因为我在 int AppDelegate 中设置了所有内容,所以我调用了 didFinishLaunchingWithOptions
12月1日更新
所以我让一切保持原样,但早上没有收到通知。 .我查看了我的代码以找出原因。有 2 个问题。
我的函数中有一行代码会在遍历我的循环时删除之前设置的任何通知,所以我将下面的代码行注释掉了,但仍然无法正常工作正如预期的那样。
UNUserNotificationCenter.current().removeAllPendingNotificationRequests()
我发现我已经用相同的 requestIdentifier 安排了我的通知,这基本上让我在最后一天只有 1 个通知。我只是通过字符串插值在自定义 ID 变量的末尾添加了索引,如下所示:
let requestId = "textNotification\(day)"