Skip to content

Instantly share code, notes, and snippets.

@mlcollard
Created April 20, 2018 12:35
Show Gist options
  • Save mlcollard/f9c590eb56ed64196fcb60aabadec8f2 to your computer and use it in GitHub Desktop.
Save mlcollard/f9c590eb56ed64196fcb60aabadec8f2 to your computer and use it in GitHub Desktop.
iOS: Notifications
// TODO: import UserNotifications
// Create the notification content
let content = UNMutableNotificationContent()
content.title = "Hi"
content.body = "Hello"
print(#function, #line)
// Configure the notification trigger for 3 seconds
var dateInfo = DateComponents()
dateInfo.hour = Calendar.current.component(.hour, from: Date())
dateInfo.minute = Calendar.current.component(.minute, from: Date()) + 1
let trigger = UNCalendarNotificationTrigger(dateMatching: dateInfo, repeats: false)
print(#function, #line)
// Create the notification request object.
let request = UNNotificationRequest(identifier: "MorningAlarm", content: content, trigger: trigger)
print(#function, #line)
// Schedule the request.
let center = UNUserNotificationCenter.current()
center.add(request) {
error in
if let error = error {
print(error.localizedDescription)
}
print(#function, #line)
}
print(#function, #line)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment