Logo for tanaschita.com

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.

Notification with text input action.
Notification with text input action

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: [])
Sponsorship logo
Preparing for a technical iOS job interview
Check out my new book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, Authentication, Core Data, Concurrency with async/await, Security, Automated Testing and more.
LEARN MORE

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

Text input action with placeholder and custom button title.
Text input action with placeholder and custom button title

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()
}
Sponsorship logo
Preparing for a technical iOS job interview
Check out my new book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, Authentication, Core Data, Concurrency with async/await, Security, Automated Testing and more.
LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

notifications

swift

ios

Developer guide on push notifications for iOS

Learn how to setup and handle remote push notifications.

21 May 2023 · 5 min read

Latest articles and tips

© 2023 tanaschita.com

Privacy policy

Impressum