UNCalendarNotificationTrigger doesn't get stored unless repeats is true

后端 未结 3 1769
Happy的楠姐
Happy的楠姐 2021-01-05 22:58

I\'ve noticed that if I create an UNCalendarNotificationTrigger with a custom date it does\'t get added unless i put: let trigger = UNCalendarNotificatio

相关标签:
3条回答
  • 2021-01-05 23:53

    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.

    0 讨论(0)
  • 2021-01-05 23:55

    On my app, I request permission after notification set by mistake. So If I want to get pending notification count, I got 0.

    I requested permission on AppDelegate but notifications setted on first view viewdidload(). I added a notification function to trigger with button. After get permission click button and finally setted my notification and I got pending notification count 1.

    I hope that's will help you.

    0 讨论(0)
  • 2021-01-06 00:03

    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
            }
        }
    
    0 讨论(0)
提交回复
热议问题