Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UNCalendarNotificationTrigger doesn't get stored unless repeats is true

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)")
      }
 }
like image 947
Piga4 Avatar asked Oct 17 '22 22:10

Piga4


1 Answers

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.

like image 57
LinShiwei Avatar answered Oct 21 '22 03:10

LinShiwei