ios - 有关 Swift 中等待批准的计划通知的问题

标签 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

            }
      }
}

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

工作日 = [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>)

我想要的是三个触发器,Weekday一一存储在[2,3,4]数组中。

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

我想问两个问题。

  1. 如果你看一下上面代码的日志,如果你有一系列Weekday存储为2的触发器,它们是一个一个执行的吗?例如,如果是星期一,那么今天听起来一次,下周一听起来一次吗?

  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)")
        }
    }
}

关于ios - 有关 Swift 中等待批准的计划通知的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59449164/

相关文章:

swift - Swift 中 pthread_mutex_lock 的替代方案?

ios - 尝试后台刷新

ios - 想要在 iOS sdk 中反转 BOOL 值

ios - navigationItem setRightBarButtonItems 间距太宽

swift - UISearchBar 不适用于数组中的子部分

xcode swift 如何拆分图像?

ios - 我想快速使视频应用程序轻量级

ios - 使用某种关系保存应用程序数据的最佳方式

ios - 更改框架时查看重新加载

swift - 过滤函数语法?