开发者社区> 问答> 正文

关于SWIFT中日程通知工作日设置的问题

我昨天发了这篇文章。

关于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倍。帮帮我。

展开
收起
游客5akardh5cojhg 2019-12-24 13:11:14 1238 0
0 条回答
写回答
取消 提交回答
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关课程

更多

相关电子书

更多
OpenStack Swift 海量小文件优化之路 立即下载
From Java/Android to Swift iOS 立即下载
Swift在Airbnb的应用实践 立即下载