除非重复为true,否则不会存储UNCalendarNotificationTrigger

Pig*_*ga4 3 ios swift swift3 ios10

我注意到,如果我创建一个UNCalendarNotificationTrigger带有自定义日期的,除非输入: let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: **true**)

苹果的例子是:

let date = DateComponents()
date.hour = 8
date.minute = 30 
let trigger = UNCalendarNotificationTrigger(dateMatching: date, repeats: true)
Run Code Online (Sandbox Code Playgroud)

可以重复== 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)
Run Code Online (Sandbox Code Playgroud)

那么我总是收到0个待处理的通知...

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

            })

num of pending notification 0
Run Code Online (Sandbox Code Playgroud)

任何的想法?

编辑1: 添加其中一个答案指出的其他上下文。我实际上是将请求添加到当前的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)")
      }
 }
Run Code Online (Sandbox Code Playgroud)

Lin*_*wei 5

我有同样的问题,现在解决。这是由于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()
    }
}
Run Code Online (Sandbox Code Playgroud)

在控制台中,打印component

year: 106 month: 2 day: 14 hour: 12 minute: 3 second: 42 isLeapMonth: false 
Run Code Online (Sandbox Code Playgroud)

并且在这种情况下,在挂起的通知列表中找不到该通知。

2.当我将component'year'明确设置为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()
    }
}
Run Code Online (Sandbox Code Playgroud)

在控制台中,component是:

year: 2017 month: 2 day: 14 hour: 12 minute: 3 second: 42 isLeapMonth: false 
Run Code Online (Sandbox Code Playgroud)

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

接下来,我检查未决的通知请求,以查找触发日期的年份部分是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()!))                    
    }
}
Run Code Online (Sandbox Code Playgroud)

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

year: 106 month: 2 day: 14 hour: 12 minute: 3 second: 42 isLeapMonth: false 
Run Code Online (Sandbox Code Playgroud)

结论

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

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

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


Mic*_*ael -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
        }
    }
Run Code Online (Sandbox Code Playgroud)