通知操作与类别
为通知添加交互式操作。
通知操作与类别 是 CoddyKit 上的免费 Swift Academy 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Swift Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Swift Academy 课程共包含 4 节课。
交互式通知
通知不只能显示文本,还可以提供按钮。用户无需打开应用即可回复消息、接受邀请或将提醒延后。这是通过将操作分组为类别实现的。
import UserNotifications
// Category = a set of actions
// Each delivered notification names its category
// so the system shows the right buttons定义操作
UNNotificationAction 包含一个标识符(稍后会用它进行匹配)、显示在按钮上的本地化标题,以及 .destructive 或 .foreground 等选项(后者会启动应用)。
import UserNotifications
let accept = UNNotificationAction(
identifier: "ACCEPT",
title: String(localized: "Accept"),
options: [.foreground])
let decline = UNNotificationAction(
identifier: "DECLINE",
title: String(localized: "Decline"),
options: [.destructive])将操作分组到类别中
UNNotificationCategory 会将操作归入一个类别标识符下。数组中操作的顺序就是按钮的顺序。请在启动时通过通知中心注册类别,这项操作只需执行一次。
import UserNotifications
let inviteCategory = UNNotificationCategory(
identifier: "INVITE",
actions: [accept, decline],
intentIdentifiers: [],
options: [])
UNUserNotificationCenter.current()
.setNotificationCategories([inviteCategory])为通知标记类别
要显示按钮,通知内容必须将 categoryIdentifier 设置为已注册的类别。对于远程推送,服务器会在 aps 字典中发送 "category": "INVITE"。
import UserNotifications
let content = UNMutableNotificationContent()
content.title = String(localized: "New invite")
content.body = String(localized: "Ada invited you")
content.categoryIdentifier = "INVITE"处理所选操作
用户点击按钮时,委托对象的 didReceive response 会执行,并将 response.actionIdentifier 设置为您的操作 ID。请根据它进行分支处理,以执行正确的工作。
import UserNotifications
func userNotificationCenter(
_ center: UNUserNotificationCenter,
didReceive response: UNNotificationResponse
) async {
switch response.actionIdentifier {
case "ACCEPT": print("accepted")
case "DECLINE": print("declined")
default: break
}
}默认与关闭标识符
系统始终存在两个标识符:UNNotificationDefaultActionIdentifier 表示用户点击正文以打开应用,UNNotificationDismissActionIdentifier 表示用户关闭了通知。请将它们与自定义操作一同处理。
import UserNotifications
func route(_ id: String) {
switch id {
case UNNotificationDefaultActionIdentifier:
print("opened the app")
case UNNotificationDismissActionIdentifier:
print("dismissed")
default:
print("custom action", id)
}
}文本输入操作
UNTextInputNotificationAction 会显示一个内嵌文本框,非常适合快速回复。它会为输入框添加按钮标题和占位提示。
import UserNotifications
let reply = UNTextInputNotificationAction(
identifier: "REPLY",
title: String(localized: "Reply"),
options: [],
textInputButtonTitle: String(localized: "Send"),
textInputPlaceholder: String(localized: "Message"))读取输入的文本
对于文本操作,响应对象是 UNTextInputNotificationResponse。请将其转换为该类型,以读取 userText,也就是用户输入的字符串。
import UserNotifications
func handle(_ response: UNNotificationResponse) {
if let textResponse = response
as? UNTextInputNotificationResponse {
let message = textResponse.userText
print("reply:", message)
}
}类别选项
类别选项可以细化行为:.customDismissAction 会将关闭事件传递给您的委托对象,.hiddenPreviewsShowTitle 会在锁定屏幕上隐藏预览时仍显示标题,而 .allowInCarPlay 允许在 CarPlay 中显示通知。
import UserNotifications
let category = UNNotificationCategory(
identifier: "MESSAGE",
actions: [reply],
intentIdentifiers: [],
options: [.customDismissAction,
.hiddenPreviewsShowTitle])
_ = category后台与前台操作
没有 .foreground 选项时,操作会在后台运行您的处理程序,而不会将应用带到前台,非常适合稍后提醒或点赞。使用 .foreground 时,应用会启动,以便您显示界面。请根据用户是否需要看到内容来选择。
import UserNotifications
let snooze = UNNotificationAction(
identifier: "SNOOZE",
title: String(localized: "Snooze"),
options: []) // background, no app launch
let open = UNNotificationAction(
identifier: "OPEN",
title: String(localized: "Open"),
options: [.foreground]) // launches app
_ = (snooze, open)完整的回复流程
将文本操作、类别以及能够区分输入回复和普通打开操作的处理程序结合起来。这种模式支持内联消息回复。
import UserNotifications
func handle(_ response: UNNotificationResponse) async {
switch response.actionIdentifier {
case "REPLY":
let text = (response as?
UNTextInputNotificationResponse)?.userText ?? ""
print("send reply:", text)
case UNNotificationDefaultActionIdentifier:
print("open conversation")
default:
break
}
}快速检查
请回顾系统如何知道要显示哪些按钮。
回顾
您已学会交互式通知:
- 创建
UNNotificationAction(包括UNTextInputNotificationAction),并将它们分组到UNNotificationCategory中。 - 在启动时注册类别;在内容上设置
categoryIdentifier(或在推送中设置category)以显示按钮。 - 处理
response.actionIdentifier,包括默认和关闭标识符,并通过userText读取回复。 - 对于稍后提醒等后台操作,请省略
.foreground。
用 AI 导师学习 Swift — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 122
- 课程
- 409
常见问题解答
「通知操作与类别」课时是免费的吗?
是的 — 「通知操作与类别」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Swift Academy 课程的其余内容,请升级到 CoddyKit PRO。 Swift Academy 课程共包含 4 节课。
「通知操作与类别」这节课中我会学到什么?
为通知添加交互式操作。 你通过在浏览器中直接运行的动手代码来练习 Swift Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Swift Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Swift Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「通知操作与类别」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Swift Academy 课中编写并运行代码吗?
能。每节 Swift Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。