swift - 如何在 Swift 中为工作周(周一至周五)设置 UNCalendarNotificationTrigger?
问题描述
使用iOS13.4、XCode11.4、Swift5.2、
您如何设置将在每个工作日(即周一至周五,但不是周六和周日)触发的本地通知?
我尝试了不同的日历组件设置,但没有成功解决工作日问题。
这是我到目前为止所做的......
如果警报需要在特定日期:
Calendar.current.dateComponents([.day, .month, .year, .hour, .minute, .second], from: notificationDate)
如果闹钟需要每天重复:
Calendar.current.dateComponents([.hour, .minute], from: notificationDate)
如果闹钟需要在特定工作日重复:
Calendar.current.dateComponents([.weekday, .hour, .minute], from: notificationDate)
等等
但是您如何设置几个工作日(例如每个工作日周一到周五)?
这是我的精确 notificationDate 示例的代码:
import UIKit
class MyViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let content = UNMutableNotificationContent()
content.title = "My Notification Title"
content.categoryIdentifier = UUID().uuidString
var notificationIdentifier = "23224-23134-234234"
var notificationDate = Date(timeInterval: 30, since: Date())
let notificationTriggerKind = Calendar.current.dateComponents([.day, .month, .year, .hour, .minute, .second], from: notificationDate)
let notificationTrigger = UNCalendarNotificationTrigger(dateMatching: notificationTriggerKind, repeats: false)
let notificationRequest = UNNotificationRequest(identifier: notificationIdentifier, content: content, trigger: notificationTrigger)
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.delegate = self
notificationCenter.add(notificationRequest) {(error) in if let error = error { print("error: \(error)") } }
}
}
extension MyViewController: UNUserNotificationCenterDelegate {
// kicks in when App is running in Foreground (without user interaction)
func userNotificationCenter(_ center: UNUserNotificationCenter,
willPresent notification: UNNotification,
withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
// ...do your custom code...
completionHandler([.alert, .sound])
}
// kicks in when App is running in Foreground or Background
// (AND App is open) AND user interacts with notification
func userNotificationCenter(_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse, withCompletionHandler
completionHandler: @escaping () -> Void) {
// ... do your custom code ....
return completionHandler()
}
}
解决方案
你走的很好。
要做到这一点,您需要为每一天安排一个通知。
检查这个例子:
- 创建和扩展日期以获取特定日期的小时、分钟和秒
extension Date {
public func getDateComponents() -> DateComponents {
let dateComponents = Calendar.current.dateComponents([ .hour, .minute, .second], from: self)
return dateComponents
}
}
- 或者直接创建一个日期组件:
var dateComponents = DateComponents()
dateComponents.hour = 9
dateComponents.minutes = 10
dateComponents.weekDay = 1
- 然后检查从星期一(2)到星期五(6)的花费天数并迭代一组工作日以创建每天的特定通知并重复。
let workDays = 2...6
for day in workDays {
var dateComponents = date.getDateComponents()
dateComponents.weekday = day
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: repeats)
let request = UNNotificationRequest(identifier: identifier, content: content, trigger: trigger)
self.notificationCenter.add(request) { (error) in
if let error = error {
}
}
}
推荐阅读
- elasticsearch - 我怎样才能让 ElasticSearch 只产生一个字段的前几个单词?
- unit-testing - LiveData 单元测试在协程和多线程下未通过,因为返回 true 而不是预期的 false
- python-3.x - 使用python的浅层CNN模型
- php - 如何在 woocommerce 产品循环(商店页面)中添加减少按钮?
- php - 在 Laravel 的另一个表中保存输入字段
- regex - 使用 C++ 用正则表达式替换子字符串匹配
- cmake - 如何在 Raspberry Pi 上构建 Paho MQTT C 静态库?
- git - 实际上从远程 Git 存储库中删除提交
- ios - 如何修复 CallKit 目录扩展中的“sqlite3”和其他错误?
- powershell - Powershell 尝试将组应用于仅适用于该文件夹的文件夹