UNCalendarNotificationTrigger doesn't get stored unless repeats is true

五迷三道 提交于 2019-11-30 23:23:16

I have the same problem and I solve it now. It is due to dateComponents' year.

In order to solve this problem, I test the following code:

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

In console, print component:

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

And in this case, the notification cannot be found in pending notification list.

2.When I set component's year explicitly to 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()
    }
}

In console, the component is:

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

Then this notification can be found in pending notification list.

And next, I check in the pending notification requests to find whether the trigger-date's year component is 106 or 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()!))                    
    }
}

I find the trigger's nextTriggerDate components are:

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

Conclusion

So if you want to set the trigger's repeats to false, you should make sure the trigger date is bigger than current date.

The default dateComponents' year may be unsuitable, such as 106. If you want the notification to fire in 2017, you should set the components year to 2017 explicitly.

Perhaps this is a bug, because I set trigger's dateComponents' year to 2017 but get 106 in nextTriggerDate of pending notification request.

UNCalendarNotificationTrigger creates the schedule for which a notification should occur, but it does not do the scheduling. For this you need to create a UNNotificationRequest and then add this to the notification center. Something along the lines of:

    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
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!