关于时间表通知在Swift中的工作日设置的问题

时间:2019-12-24 02:29:10

标签: ios swift notifications

我昨天发布了这篇文章。

Questions about schedule notifications pending approval in 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倍。帮帮我。

0 个答案:

没有答案