有关在Swift中待批准的计划通知的问题

时间:2019-12-23 00:47:38

标签: ios swift

我创建了一个功能,可以在您设置的一周中的特定时间和日期发出警报。

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 trigger = UNCalendarNotificationTrigger.init(dateMatching: dateComponents, repeats: true)

       let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)
            center.add(request)
            center.getPendingNotificationRequests { (requests) in
                for request in requests {
                    print("trigger : \(request.trigger)")
                }
            }

            UNUserNotificationCenter.current().add(request) { (error) in

            }
      }
}

您可以在上面的代码中看到,要重复的日子是星期一,星期二和星期三。

weekdays = [2,3,4]

之后,我想使用[2,3,4]数组上的for语句创建一个在星期一,星期二和星期三发出的警报。

但是警报没有按我想要的响起。

我使用getPendingNotificationRequests方法将预定警报列表打印到控制台。

trigger : Optional(<UNCalendarNotificationTrigger: 0x283242f20; dateComponents: <NSDateComponents: 0x2830567a0> {
    Hour: 6
    Minute: 40
    Weekday: 2, repeats: YES>)
trigger : Optional(<UNCalendarNotificationTrigger: 0x283241920; dateComponents: <NSDateComponents: 0x283056900> {
    Hour: 6
    Minute: 40
    Weekday: 2, repeats: YES>)
trigger : Optional(<UNCalendarNotificationTrigger: 0x2832416a0; dateComponents: <NSDateComponents: 0x2830556f0> {
    Hour: 6
    Minute: 40
    Weekday: 3, repeats: YES>)
trigger : Optional(<UNCalendarNotificationTrigger: 0x283242f20; dateComponents: <NSDateComponents: 0x283056900> {
    Hour: 6
    Minute: 40
    Weekday: 2, repeats: YES>)
trigger : Optional(<UNCalendarNotificationTrigger: 0x2832426e0; dateComponents: <NSDateComponents: 0x283054c40> {
    Hour: 6
    Minute: 40
    Weekday: 3, repeats: YES>)
trigger : Optional(<UNCalendarNotificationTrigger: 0x2832413c0; dateComponents: <NSDateComponents: 0x283056930> {
    Hour: 6
    Minute: 40
    Weekday: 4, repeats: YES>)

我想要的是三个触发器,而工作日是在[2,3,4]数组中一个接一个地存储的。

但是,创建了六个触发器,并且工作日的顺序存储不正确。

我想问两个问题。

  1. 如果查看上述代码的日志,如果您有一系列将Weekday存储为2的触发器,它们是一个接一个地执行吗?例如,今天是星期一,下周一是一次?

  2. 为什么警报存储如此错误,如何解决该问题?

1 个答案:

答案 0 :(得分:2)

获得六个触发器的原因是,在for循环(for weekday in weekdays)中,您将每个触发器添加了两次。每个触发器在行center.add(request)处添加一次,并在行UNUserNotificationCenter.current().add(request) { (error) in处添加一次。您可以通过更新for循环来解决此问题,如下所示:

for weekday in weekdays {
    var dateComponents = DateComponents()
    dateComponents.hour = hour
    dateComponents.minute = minute
    dateComponents.weekday = weekday

    let trigger = UNCalendarNotificationTrigger.init(dateMatching: dateComponents, repeats: true)

    let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger)

    center.current().add(request) { (error) in
        if error != nil {
            print(error.localizedDescription)
        }
    }
}

您可能还只想在for循环完成后 打印触发器,而不是在for循环内。因此,我将在for循环之后添加以下代码:

func scheduleNotification() {

    ...

    for weekday in weekdays {

        ...

    }

    //This following code was originally within your for loop, but should probably execute after the for loop instead
    //The console should look a lot cleaner after doing so
    center.getPendingNotificationRequests { (requests) in
        for request in requests {
            print("trigger : \(request.trigger)")
        }
    }
}