【发布时间】:2017-12-17 02:19:20
【问题描述】:
我正在尝试为一周中的特定日期(例如星期一、星期三等)安排本地通知,然后每周重复一次。 这是设置通知的屏幕的外观:
用户可以选择通知时间和重复天数。
我安排单个非重复通知的方法如下所示:
static func scheduleNotification(reminder: Reminder) {
// Setup notification content.
let content = UNMutableNotificationContent()
//content.title = NSString.localizedUserNotificationString(forKey: "Reminder", arguments: nil)
content.body = NSString.localizedUserNotificationString(forKey: reminder.reminderMessage, arguments: nil)
content.sound = UNNotificationSound.default()
// Configure the triger for specified time.
//
let dateComponentes = reminder.dateComponents
// TODO: Configure repeating alarm
// For the testing purposes we will not repeat the reminder
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponentes, repeats: false)
// Create the request object.
let request = UNNotificationRequest(identifier: "\(reminder.reminderId)", content: content, trigger: trigger)
// Schedule the request.
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.add(request) { (error: Error?) in
if let theError = error {
print(theError.localizedDescription)
}
}
}
从 UIDatePicker 小部件中提取日期组件并存储在提醒类中:
let date = reminderTimeDatePicker.date.addingTimeInterval(60 * 60 * 24 * 7)
let components = Calendar.current.dateComponents([.weekday, .hour, .minute], from: date)
...
reminder.dateComponents = components
我有一个数组selectedDays[Int](作为提醒类的属性)来保存应该触发通知的星期几的信息。
如何在一周中的特定日期安排通知以及如何每周重复一次?
即使是一条评论也会有所帮助,并提前感谢您。
【问题讨论】:
-
您是否设法为多个工作日创建通知?还是必须单独创建? @吉迪恩
-
经过大量研究后,我发现您必须在每个工作日单独执行它们,或者您可以创建每日通知。但也许你可以,而且我没有做足够的研究。尽管如此,据我所知,你必须分开做。但是,如果您设法找到在多个工作日执行单个通知的方法,请告诉我。
标签: ios swift swift3 notifications