Skip to content

Instantly share code, notes, and snippets.

func didReceive(_ response: UNNotificationResponse, completionHandler completion: @escaping (UNNotificationContentExtensionResponseOption) -> Void)
{
if response.actionIdentifier == "accept"
{
print("Accept - from Extension")
DispatchQueue.main.async {
completion(.dismissAndForwardAction)
}
}
else
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
}
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)
}
{
"aps": {
"alert": {
"title": "Invitation",
"subtitle": "This is a Remote Notification.",
"body": "You are invited."
},
"category": "INVITATION",
"sound": "default",
"content-available": 1,
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)
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
{
//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.
}
//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)
//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.