UNCalendarNotificationTrigger doesn't get stor

2020-03-04 05:48发布

I've noticed that if I create an UNCalendarNotificationTrigger with a custom date it does't get added unless i put: let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: **true**)

Apple Example is:

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

which make sense to be repeats == true.

In my scenario I dont need to create one notification that gets repeated many times, but I need multiple notificaitons fired only once on a specific calendar date (which is of course in the future)..

If I'm doing:

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)

then i always get 0 pending notifications...

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

            })

num of pending notification 0

Any idea?

EDIT1: Adding other context as pointed out by one of the answers. I'm actually adding the request to the current 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)")
      }
 }

2条回答
祖国的老花朵
2楼-- · 2020-03-04 06:30

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
        }
    }
查看更多
不美不萌又怎样
3楼-- · 2020-03-04 06:39

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.

查看更多
登录 后发表回答