Created
August 2, 2017 16:51
-
-
Save rkotzy/70d9b4cd99eca4daa2bfdbdd8571af97 to your computer and use it in GitHub Desktop.
Fire Local Notifications in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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