2

I have problems setting a notification of type UNCalendarNotificationTrigger that is determined by DateComponents

//set content
let content = UNMutableNotificationContent()
content.title = "My Notification Management Demo"
content.subtitle = "Timed Notification"
content.body = "Notification"
content.body = "Notification pressed \(pressed) times"
content.categoryIdentifier = "message"

//set trigger possible problem
let formatted = DateFormatter()
formatted.dateFormat = "yyyy-MM-dd'T'HH:mm:ssxxxxx"
formatted.date(from: "2018-01-19T02:29:50+0000")

var dateInfo = DateComponents()
dateInfo.calendar = formatted.calendar

let trigger = UNCalendarNotificationTrigger(
    dateMatching: dateInfo,
    repeats: false
)

//Create the request
let request = UNNotificationRequest(
    identifier: "my.notification",
    content: content,
    trigger: trigger
 )

 //Schedule the request
 UNUserNotificationCenter.current().add(request) { (error : Error?) in
     print("all ok")
 }

And when I make a request to get the pending notifications, the array is wide.

  UNUserNotificationCenter.current().getPendingNotificationRequests(completionHandler: {requests -> () in
        print("\(requests.count) requests -------")
        for request in requests{
            print(request.identifier)
        }
    })
  • having the same, simpler datecomponents such as hour = 20 and minute 10 . and not showing anything, i can see the request.trigger by getPendingnotifications... but the hour is at 19 all the time... :/ – lorenzo gonzalez Jun 13 '18 at 01:14
  • Have you checked the error returned from `UNUserNotificationCenter.current().add`? Your DateComponents has only a calendar, which really doesn't constrain the trigger. – Chris Prince Jul 18 '19 at 19:17

0 Answers0