Skip to content

Instantly share code, notes, and snippets.

let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.badge, .sound, .alert]) { (granted, error) in
//granted = yes, if app is authorized for all of the requested interaction types
//granted = no, if one or more interaction type is disallowed
}
//Actions
let remindLaterAction = UNNotificationAction(identifier: "remindLater", title: "Remind me later", options: UNNotificationActionOptions(rawValue: 0))
let acceptAction = UNNotificationAction(identifier: "accept", title: "Accept", options: .foreground)
let declineAction = UNNotificationAction(identifier: "decline", title: "Decline", options: .destructive)
let commentAction = UNTextInputNotificationAction(identifier: "comment", title: "Comment", options: .authenticationRequired, textInputButtonTitle: "Send", textInputPlaceholder: "Share your thoughts..")
//Category
let invitationCategory = UNNotificationCategory(identifier: "INVITATION", actions: [remindLaterAction, acceptAction, declineAction, commentAction], intentIdentifiers: [], options: UNNotificationCategoryOptions(rawValue: 0))
//Register the app’s notification types and the custom actions that they support.
//Notification Content
let content = UNMutableNotificationContent()
content.title = "Invitation"
content.subtitle = "This is a Local Notification."
content.body = "You are invited."
content.categoryIdentifier = "INVITATION"
content.sound = UNNotificationSound.default()
//Notification Trigger - when the notification should be fired
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
//Notification Content
let content = UNMutableNotificationContent()
content.title = "Invitation"
content.subtitle = "This is a Local Notification."
content.body = "You are invited."
content.categoryIdentifier = "INVITATION"
content.sound = UNNotificationSound.default()
//Notification Trigger - when the notification should be fired
let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5, repeats: false)
//Here you decide whether to silently handle the notification or still alert the user.
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
//Write you app specific code here
completionHandler([.alert, .sound]) //execute the provided completion handler block with the delivery option (if any) that you want the system to use. If you do not specify any options, the system silences the notification.
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
{
switch response.notification.request.content.categoryIdentifier
{
case "GENERAL":
break
case "INVITATION":
switch response.actionIdentifier
{
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
let tokenParts = deviceToken.map { data -> String in
return String(format: "%02.2hhx", data)
}
let token = tokenParts.joined()
print("Device Token: \(token)")
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error)
{
"aps": {
"alert": {
"title": "Invitation",
"subtitle": "This is a Remote Notification.",
"body": "You are invited."
},
"category": "INVITATION",
"sound": "default",
"content-available": 1,
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void)
{
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent
{
// Modify the notification content here...
bestAttemptContent.body = "Address: Sea Shells Apartments, Mumbai" //Here..!!!
contentHandler(bestAttemptContent)
}
func didReceive(_ notification: UNNotification)
{
self.titleLabel?.text = notification.request.content.title
self.subTitleLabel?.text = notification.request.content.subtitle
self.bodyLabel?.text = notification.request.content.body
}