Skip to content

Instantly share code, notes, and snippets.

@youvalv
Last active April 5, 2017 14:37
Show Gist options
  • Save youvalv/e9158aea242d206fe053fa416cfddc35 to your computer and use it in GitHub Desktop.
Save youvalv/e9158aea242d206fe053fa416cfddc35 to your computer and use it in GitHub Desktop.
Displaying Notification
private func setupNotificationsSettings(alertTitle: String, alertBody: String, identifier: String, fireDate: Date, repeatInterval: NSCalendar.Unit) {
if #available(iOS 10.0, *) {
let specificDate = fireDate
//convert dateComponents() to Date()
let dateComponents = Calendar.current.dateComponents([.year, .month, .day, .hour, .minute, .second, .timeZone], from: specificDate)
var trigger: UNNotificationTrigger
if (repeatInterval.rawValue > 0) {
trigger = UNTimeIntervalNotificationTrigger(timeInterval: TimeInterval(repeatInterval.rawValue), repeats: false)
} else {
trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: false)
}
let content = UNMutableNotificationContent()
content.title = alertTitle
content.subtitle = "omg subtitle"
content.body = alertBody
content.sound = UNNotificationSound.default()
content.categoryIdentifier = identifier
content.userInfo = ["identifier" : identifier]
let notification = UNNotificationRequest(identifier: "silent push event", content: content, trigger: trigger)
UNUserNotificationCenter.current().add(notification, withCompletionHandler: { (error:Error?) in
})
} else {
let sharedApp = UIApplication.shared
let notification = UILocalNotification()
notification.alertTitle = alertTitle
notification.alertBody = alertBody
notification.fireDate = fireDate
notification.category = identifier
notification.userInfo = ["identifier" : identifier]
notification.repeatInterval = repeatInterval
notification.timeZone = TimeZone.current
sharedApp.scheduleLocalNotification(notification)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment