UNCalendarNotificationTrigger doesn't get stored unless repeats is true
Asked Answered
S

3

5

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)")
      }
 }
Spontaneity answered 16/11, 2016 at 4:30 Comment(0)
R
9

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.

Reputed answered 14/2, 2017 at 4:55 Comment(0)
H
0

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.

Hydrokinetic answered 15/4, 2020 at 9:18 Comment(0)
O
-3

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
        }
    }
Opinicus answered 16/11, 2016 at 5:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.