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
Join the FREE iOS Architect Crash Course (for a limited time!)
If you’re a mid/senior iOS developer who’s looking to improve both your skills and salary level, then join this 100% free online crash course. Hurry up because it's available only until November 27th!
Click to get it now

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
Join the FREE iOS Architect Crash Course (for a limited time!)
If you’re a mid/senior iOS developer who’s looking to improve both your skills and salary level, then join this 100% free online crash course. Hurry up because it's available only until November 27th!
Click to get it now

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

notifications

swift

ios

Quick guide on local notifications for iOS

Learn how to schedule local notifications in different ways and how to handle them.

02 May 2022 · 6 min read

Latest articles and tips

© 2022 tanaschita.com

Privacy policy

Impressum