Home > AI > IOS > UserNotifications >

UNCalendarNotificationTrigger

Example : This code shows press the button will trigger to send the notification in 5 seconds. Remember the app needs to be Background Mode.

func sendLocalNote() {
    // payload
    let content = UNMutableNotificationContent()
    content.title = NSString.localizedUserNotificationString(forKey: "Local Note Title!", arguments: nil)
    content.subtitle = "subtitle"
    content.body = NSString.localizedUserNotificationString(forKey: "Local Note Body", arguments: nil)
    content.sound = .default
    content.badge = 1

    // trigger
    let date = Date().addingTimeInterval(5)
    let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date)
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)

    // request
    let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger) // Schedule the notification.
    center.add(request) { (error : Error?) in
        if error != nil {
            // Handle any errors
        }
        print("local note sent!")
    }
}

Leave a Reply