Skip to content

Instantly share code, notes, and snippets.

@rkotzy
Created August 2, 2017 16:51
Show Gist options
  • Save rkotzy/70d9b4cd99eca4daa2bfdbdd8571af97 to your computer and use it in GitHub Desktop.
Save rkotzy/70d9b4cd99eca4daa2bfdbdd8571af97 to your computer and use it in GitHub Desktop.
Fire Local Notifications in Swift
func fireNotification(title: String?, body: String?, id: String) {
let content = UNMutableNotificationContent()
if let title = title {
content.title = title
}
if let body = body {
content.body = body != "" ? body : "arrival"
}
content.sound = UNNotificationSound.default()
let request = UNNotificationRequest.init(identifier: id, content: content, trigger: nil)
// Schedule the notification.
center.add(request, withCompletionHandler: { (error) in
if error != nil {
print("Something went wrong on ping for \(title): \(error)")
}
})
}
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
completionHandler(UNNotificationPresentationOptions.alert)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment