How to add text input actions to iOS push and local notifications
Learn how to define and handle a notification action that allows custom user text input.
28 Mar 2022 · 2 min read
In the last guide on how to add and handle actions for iOS notifications, we looked at how to add action buttons to a local or push notification in Swift so users can respond to a delivered notification without launching the app.
The system also provides us with the possibility to add a notification action that allows custom user text input.

Let's look at how to add and handle text input actions.
Add text input action to notification
A text input action is represented by a UNTextInputNotificationAction. Initialising it works exactly the same way as with UNNotificationAction types.
let howManyGlassesInputAction = UNTextInputNotificationAction(identifier: "drinkingReminder.howManyGlassesInputAction",title: "How many glasses of water did you drink?",options: [])

UNTextInputNotificationAction also provides an alternative initializer that takes additional parameters textInputButtonTitle and textInputPlaceholder.

After initializing the action, we add it to a category like we have done with UNNotificationAction types and register the category with UNUserNotificationCenter.
let drinkingRemiderCategory = UNNotificationCategory(identifier: "drinkingReminderCategory",actions: [doneAction, notThirstyAction, howManyGlassesInputAction],intentIdentifiers: [],options: .customDismissAction)UNUserNotificationCenter.current().setNotificationCategories([drinkingRemiderCategory])
Now, every time a notification with the id drinkingReminderCategory is delivered, the text input action is shown to the user.
Handle response
When a user chooses the text input action and provides an input, the text is included in the response object delivered to our app. To access it, we try to map the response to the UNTextInputNotificationResponse type that has the property userText.
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {switch response.actionIdentifier {case "drinkingReminder.howManyGlassesInputAction":if let userInput = (response as? UNTextInputNotificationResponse)?.userText {// Handle text input}default:break}completionHandler()}

Newsletter
Like to support my work?
Say hi
Related tags
Articles with related topics
Latest articles and tips