我昨天发了这篇文章。
关于SWIFT中待批准的计划通知的问题
幸运的是,昨天的帖子解决了这个问题。(重复触发器的问题)
但我还是把闹钟设在星期一,下午6点40分,然后推到周一,但不是在星期一,韦德。
我试图用另一种方式解决这个问题。
代码:
func scheduleNotification() {
let center = UNUserNotificationCenter.current()
let hour = 6
let minute = 40
let weekdays = [2,3,4] // mon, tue, wed
let content = UNMutableNotificationContent()
content.title = "Fire!!"
content.body = "test.!"
content.badge = 1
content.categoryIdentifier = "alarm"
content.userInfo = ["customData": "fizzbuzz"]
content.sound = UNNotificationSound.default
for weekday in weekdays {
var dateComponents = DateComponents()
dateComponents.hour = hour
dateComponents.minute = minute
dateComponents.weekday = weekday
let date = Calendar.current.date(from: dateComponents)
let triggerWeekly = Calendar.current.dateComponents([.weekday, .hour, .minute], from: date!)
let trigger = UNCalendarNotificationTrigger.init(dateMatching: triggerWeekly, repeats: true)
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
center.add(request) { (error) in
if error != nil {
print("error.localizedDescription : ",error?.localizedDescription)
}
}
}
center.getPendingNotificationRequests { (requests) in
for request in requests {
print("trigger: \(request.trigger)")
}
}
}
我想用上面的代码重复一天。但当我把预定的触发器打印到控制台时,
trigger : Optional(<UNCalendarNotificationTrigger: 0x283242f20; dateComponents: <NSDateComponents: 0x2830567a0> {
Hour: 6
Minute: 40
Weekday: 7, repeats: YES>)
trigger : Optional(<UNCalendarNotificationTrigger: 0x283241920; dateComponents: <NSDateComponents: 0x283056900> {
Hour: 6
Minute: 40
Weekday: 7, repeats: YES>)
trigger : Optional(<UNCalendarNotificationTrigger: 0x2832416a0; dateComponents: <NSDateComponents: 0x2830556f0> {
Hour: 6
Minute: 40
Weekday: 7, repeats: YES>)
上面的日志被拿走了。我想要的日志是平日应该是2,3,4,但我不知道为什么7是3倍。帮帮我。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。