The existing answers are old and use deprecated frameworks.
Here's how to achieve this with the latest User Notifications framework (iOS 10.0+).
// AppDelegate.swift
static let sUserNotificationCenter: UNUserNotificationCenter = UNUserNotificationCenter.current()
// Notification action IDs
static let sAcceptActionID: String = "ACCEPT_ACTION"
static let sTentativeActionID: String = "TENTATIVE_ACTION"
static let sDeclineActionID: String = "DECLINE_ACTION"
static let sOtherResponseActionID: String = "OTHER_RESPONSE_ACTION"
// Notification category IDs
static let sMeetingInviteID: String = "MEETING_INVITE"
func RegisterNotificationCategories() {
// Specify the actions (buttons) to a notification
let meeting_accept: UNNotificationAction = UNNotificationAction(identifier: AppDelegate.sAcceptActionID, title: "ACCEPT")
let meeting_tentative: UNNotificationAction = UNNotificationAction(identifier: AppDelegate.sTentativeActionID, title: "TENTATIVE")
let meeting_decline: UNNotificationAction = UNNotificationAction(identifier: AppDelegate.sDeclineActionID, title: "DECLINE")
// An action button which accepts a text from user.
let other_response: UNTextInputNotificationAction = UNTextInputNotificationAction(identifier: AppDelegate.sOtherResponseActionID, title: "OTHER RESPONSE", textInputButtonTitle: "Enter", textInputPlaceholder: "placeholder")
// Create the notification category object
let meeting_invite: UNNotificationCategory = UNNotificationCategory(identifier: AppDelegate.sMeetingInviteID, actions: [meeting_accept, meeting_tentative, meeting_decline, other_response], intentIdentifiers: [], hiddenPreviewsBodyPlaceholder: "Preview")
// Register the notification category
AppDelegate.sUserNotificationCenter.setNotificationCategories([meeting_invite])
}
// Log(_:) is my own custom logger function.
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive pResponse: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
// obtain user's action on the notification.
let user_action: String = pResponse.actionIdentifier
Log("pResponse.actionIdentifier = " + user_action)
if(user_action == TWAppDelegate.sOtherResponseActionID) {
// If user has given a text response, the received response object will be of type
// UNTextInputNotificationResponse, which has the user's text within
let text_response: UNTextInputNotificationResponse = pResponse as! UNTextInputNotificationResponse
Log(String(format: "User's text response = %@", text_response.userText))
}
// Call completion handler to let system know that processing is complete.
completionHandler()
}
Result:
Docs: Declare your actionable notification types