【问题标题】:UNCalendarNotificationTrigger doesn't get stored unless repeats is true除非重复为真,否则不会存储 UNCalendarNotificationTrigger
【发布时间】:2017-03-30 04:17:00
【问题描述】:

我注意到,如果我创建一个带有自定义日期的UNCalendarNotificationTrigger,除非我输入以下内容,否则它不会被添加: let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: **true**)

苹果的例子是:

let date = DateComponents()
date.hour = 8
date.minute = 30 
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)

重复是有意义的 == true。

在我的场景中,我不需要创建一个重复多次的通知,但我需要在特定日历日期(当然是在未来)仅触发一次的多个通知..

如果我在做:

let calendar = Calendar(identifier: .gregorian)

let formatter = DateFormatter()
formatter.dateFormat = "yyyyMMdd"
let newdate = formatter.date(from: "20161201")

let components = calendar.dateComponents(in: .current, from: newdate!)

let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: false)

然后我总是收到 0 个待处理通知...

    UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: { (notifications) in
                print("num of pending notifications \(notifications.count)")

            })

num of pending notification 0

有什么想法吗?

EDIT1: 添加答案之一指出的其他上下文。 我实际上是在将请求添加到当前的 UNUserNotificationQueue。

 let request = UNNotificationRequest(identifier: "future_calendar_event_\(date_yyyyMMdd)", content: content, trigger: trigger)

 UNUserNotificationCenter.current().add(request) { error in
      if let error = error {
           // Do something with error
           print(error.localizedDescription)
      } else {
           print("adding \((request.trigger as! UNCalendarNotificationTrigger).dateComponents.date)")
      }
 }

【问题讨论】:

    标签: ios swift swift3 ios10


    【解决方案1】:

    在我的应用程序上,我在错误设置通知后请求权限。所以如果我想获得待处理的通知计数,我得到了 0。

    我请求了 AppDelegate 的权限,但在第一个视图 viewdidload() 上设置了通知。我添加了一个通知功能来触发按钮。获得许可后点击按钮,最后设置了我的通知,我得到了待处理的通知计数 1。

    希望对你有帮助。

    【讨论】:

      【解决方案2】:

      我有同样的问题,我现在解决了。这是由于 dateComponents 的年份。

      为了解决这个问题,我测试了以下代码:

      1.

      let notificationCenter = UNUserNotificationCenter.current()
      let notificationDate = Date().addingTimeInterval(TimeInterval(10))
      let component = calendar.dateComponents([.year,.day,.month,.hour,.minute,.second], from: notificationDate)
      print(component)
      let trigger = UNCalendarNotificationTrigger(dateMatching: component, repeats: false)
      let request = UNNotificationRequest(identifier: item.addingDate.description, content: content, trigger: trigger)
      self.notificationCenter.add(request){(error) in
          if let _ = error {
              assertionFailure()
          }
      }
      

      在控制台中,打印component:

      year: 106 month: 2 day: 14 hour: 12 minute: 3 second: 42 isLeapMonth: false 
      

      在这种情况下,在待处理通知列表中找不到通知。

      2.当我将component的年份明确设置为2017年时:

      let notificationDate = Date().addingTimeInterval(TimeInterval(10))
      var component = calendar.dateComponents([.year,.day,.month,.hour,.minute,.second], from: notificationDate)
      component.year = 2017
      print(component)
      let trigger = UNCalendarNotificationTrigger(dateMatching: component, repeats: false)
      let request = UNNotificationRequest(identifier: item.addingDate.description, content: content, trigger: trigger)
      self.notificationCenter.add(request){(error) in
          if let _ = error {
              assertionFailure()
          }
      }
      

      在控制台中,component 是:

      year: 2017 month: 2 day: 14 hour: 12 minute: 3 second: 42 isLeapMonth: false 
      

      然后可以在待处理通知列表中找到此通知。

      接下来,我检查待处理的通知请求,以确定触发日期的年份组件是 106 还是 2017:

      notificationCenter.getPendingNotificationRequests(){[unowned self] requests in
          for request in requests {
              guard let trigger = request.trigger as? UNCalendarNotificationTrigger else {return}                       
              print(self.calendar.dateComponents([.year,.day,.month,.hour,.minute,.second], from: trigger.nextTriggerDate()!))                    
          }
      }
      

      我发现触发器的 nextTriggerDate 组件是:

      year: 106 month: 2 day: 14 hour: 12 minute: 3 second: 42 isLeapMonth: false 
      

      结论

      因此,如果要将触发器的重复设置为 false,则应确保触发器日期大于当前日期。

      默认日期组件的年份可能不合适,例如 106。如果您希望在 2017 年触发通知,则应将组件年份明确设置为 2017。

      也许这是一个错误,因为我将触发器的 dateComponents 年份设置为 2017 年,但在未决通知请求的 nextTriggerDate 中得到 106。

      【讨论】:

        【解决方案3】:

        UNCalendarNotificationTrigger 创建应该发生通知的计划,但它不执行计划。为此,您需要创建一个UNNotificationRequest,然后将其添加到通知中心。大致如下:

            let request = UNNotificationRequest(identifier: "MyTrigger", content: content, trigger: trigger)
            UNUserNotificationCenter.current().add(request) { error in
                if let error = error {
                    // Do something with error
                } else {
                    // Request was added successfully
                }
            }
        

        【讨论】:

          猜你喜欢
          • 2013-03-02
          • 1970-01-01
          • 2021-05-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多