移动应用中的 Notification Action:什么是、类型和设置

作者: IT Sectr 发布日期: 2026-03-21 阅读时间: 8 分钟

Notification Action — 这是显示在推送通知中的交互式按钮和输入框,允许用户无需打开应用即可执行操作。这是用户体验的关键元素,缩短了到目标屏幕的路径。根据 Android Developers, 2025Notification Action 通过减少交互磨擦力,可将用户参与度提升最多 40%。

核心要点

  • Notification Action — 推送通知中的交互式元素,用于快速执行任务。
  • 在 Android 上,操作通过 PendingIntent 和 NotificationCompat.Action.Builder 实现。
  • 在 iOS 上,操作通过 UNNotificationAction 和 UNNotificationCategory 配置。
  • 支持按钮、文本输入、“已完成”标记和延迟操作。
  • 每个操作必须在点击时在应用的 回调 中处理。

什么是 Notification Action

Notification Action — 是添加到推送通知中用于执行特定操作的交互式界面元素。用户可以无需进入应用即可回复消息、确认任务或打开特定屏幕。操作以按钮形式显示在通知文本下方,或在滑动时的扩展视图中显示。

为什么通知中需要操作

Notification Action 的主要目标是减少用户达到目标所需的步骤数。用户无需打开应用并导航到目标屏幕,只需点击一个按钮。研究表明,具有 交互式通知 的应用相比普通通知,用户参与度高出 25–40%。

不同平台上的视觉呈现

在 Android 上,Notification Action 在简约视图中显示为图标按钮,在扩展模式下显示为文字按钮。在 iOS 上,操作通过长按或向左滑动显示。每个平台对操作数量都有自己的建议—Android 建议不超过 3 个,iOS 不超过 4 个。

平台最大操作数显示方式操作类型
Android3图标 + 文字按钮、文本输入
iOS4长按 / 滑动按钮、文本输入

Android 上的 Notification Action

Android 上,Notification Action 通过 NotificationCompat.Builder 使用 addAction() 方法创建。每个操作包含一个图标、文本和一个在点击时触发的 PendingIntent。从 Android 7.0 (API 24) 开始,支持通过 RemoteInput 进行直接文本输入。

创建基本操作

要在通知中创建按钮,必须定义一个在点击时启动的 PendingIntent。PendingIntent 可以打开 Activity、启动 Service 或 BroadcastReceiver。操作图标必须是单色的,并符合 Material Design。

kotlin
val acceptIntent = Intent(context, AcceptActionReceiver::class.java)
acceptIntent.putExtra("notification_id", notificationId)

val acceptPendingIntent = PendingIntent.getBroadcast(
    context, requestCode,
    acceptIntent, PendingIntent.FLAG_UPDATE_CURRENT
)

val notification = NotificationCompat.Builder(context, CHANNEL_ID)
    .setSmallIcon(R.drawable.ic_notification)
    .setContentTitle("确认请求")
    .setContentText("确认任务执行")
    .addAction(R.drawable.ic_accept, "确认", acceptPendingIntent)
    .build()

带文本输入的操作 (RemoteInput)

对于需要文本输入的场景(例如回复消息),Android 提供了 RemoteInput。用户直接在通知中输入文本,应用无需打开 Activity 即可接收。RemoteInput 在 Android 7.0+ 上支持,并需要在 BroadcastReceiver 或 Service 中进行显式处理。

kotlin
val remoteInput = RemoteInput.Builder("reply_input")
    .setLabel("输入回答")
    .build()

val replyAction = NotificationCompat.Action.Builder(
    R.drawable.ic_reply, "回复", replyPendingIntent
)
    .addRemoteInput(remoteInput)
    .build()

val notification = NotificationCompat.Builder(context, CHANNEL_ID)
    .addAction(replyAction)
    .build()

在 Android 上处理操作

点击操作时,系统会启动指定的 PendingIntent。要从 RemoteInput 获取输入的文本,请调用 RemoteInput.getResultsFromIntent(intent)。建议在 IntentService 或 WorkManager 中处理操作,以免阻塞 UI 线程。

iOS 上的 Notification Action

iOS 上,Notification Action 通过 UserNotifications 框架实现。开发者创建操作分类 (UNNotificationCategory) 并在应用启动时进行注册。每个操作由具有唯一标识符的 UNNotificationAction 对象定义。

在 iOS 上配置分类和操作

要在 iOS 的推送通知中添加操作,需要创建一个将相关操作分组的分类。分类在通知负载中通过 category 字段指定。接收到通知后,系统显示指定分类中可用的操作。

swift
import UserNotifications

class NotificationSetup {

    func registerNotificationCategories() {
        let approveAction = UNNotificationAction(
            identifier: "APPROVE_ACTION",
            title: "确认",
            options: [.foreground]
        )

        let declineAction = UNNotificationAction(
            identifier: "DECLINE_ACTION",
            title: "拒绝",
            options: [.destructive]
        )

        let category = UNNotificationCategory(
            identifier: "REQUEST_CATEGORY",
            actions: [approveAction, declineAction],
            intentIdentifiers: [],
            options: []
        )

        UNUserNotificationCenter.current()
            .setNotificationCategories([category])
    }
}

在 iOS 上处理操作

当用户点击操作时,系统会在 UNUserNotificationCenter 的代理中调用 userNotificationCenter:didReceiveNotificationResponse 方法。response.actionIdentifier 包含执行的操作标识符。对于文本输入,使用 UNTextInputNotificationAction,它提供用户输入的文本。

swift
extension AppDelegate: UNUserNotificationCenterDelegate {

    func userNotificationCenter(
        center: UNUserNotificationCenter,
        didReceive response: UNNotificationResponse,
        withCompletionHandler completionHandler: @escaping () -> Void
    ) {
        switch response.actionIdentifier {
        case "APPROVE_ACTION":
            Log.d("Action: approved")
        case "DECLINE_ACTION":
            Log.d("Action: declined")
        case UNNotificationDefaultActionIdentifier:
            Log.d("Action: opened app")
        default:
            break
        }
        completionHandler()
    }
}

Notification Action 类型和场景

Notification Action 根据交互类型和使用场景进行分类。正确选择操作类型直接影响用户体验和目标操作的转化率。

启动应用的按钮

最常见的类型—在特定屏幕上打开应用的按钮。在 Android 上使用 PendingIntent.getActivity(),在 iOS 上使用 UNNotificationAction 的 .foreground 选项。适用于需要完整界面的操作,例如查看订单详情。

后台操作

无需打开应用即可执行的操作。在 Android 上使用 PendingIntent.getBroadcast() 或 PendingIntent.getService()。在 iOS 上—.authenticationRequired 选项或无 .foreground。示例:将任务标记为已完成、点赞文章、添加到收藏夹。

文本输入

允许用户直接在通知中输入文本。在 Android 上通过 RemoteInput 实现,在 iOS 上通过 UNTextInputNotificationAction 实现。用于回复消息、评论、输入一次性确认码。

破坏性操作

不可逆地修改数据的操作—删除、拒绝、封锁。在 Android 上视觉上突出显示(某些系统中显示为红色),在 iOS 上需要 .destructive 选项。建议对破坏性操作请求确认,例如通过额外对话框或第二次点击。

带文本输入的输入操作

一个独特的操作类别—从通知中直接文本输入。在 Android 上通过 RemoteInput 与 PendingIntent 组合实现。在 iOS 上使用继承自 UNNotificationAction 的 UNTextInputNotificationAction。这类操作用于消息应用中的 快速回复、输入促销代码、填写反馈表单或评估服务质量。输入的文本与操作标识符一起传递给应用。要在两个平台上正确处理文本输入,需要在执行目标操作之前实现对输入数据的解析和验证。

点击处理和日志记录

正确的 Notification Action 处理 对分析和用户体验至关重要。每次点击必须被记录,操作必须可靠地执行,即使应用已关闭。

分析和跟踪

通过分析系统跟踪每次 Notification Action 的点击。Firebase Analytics、Mixpanel 或 Yandex.Metrica 可以记录操作标识符、点击时间和通知上下文。这些数据有助于优化 通知策略 并提高参与度。使用不同操作组进行 A/B 测试有助于确定最有效的用户交互场景。

应用关闭时的可靠处理

如果应用已关闭,系统仍然会在点击操作时传递 Intent 或 UNNotificationResponse。在 Android 上,使用 BroadcastReceiver 以确保处理。在 iOS 上,系统会在后台启动应用并将响应传递给代理。对于关键操作(支付确认、授权),添加重试机制并通知用户操作已成功执行。

Android 上按钮的视觉自定义

从 Android 7.0 开始,开发者可以通过图标和颜色影响 Notification Action 的显示。图标必须是单色的(透明通道),大小为 24x24 dp。在 iOS 上,按钮的自定义受限—使用系统颜色和字体。Material Design 建议按优先级对操作进行分组:最重要的操作在前,破坏性操作在后。

常见问题

一个通知可以添加多少个操作?

在 Android 上建议不超过 3 个操作,在 iOS 上最多 4 个。超出限制将导致部分操作不显示或隐藏在子菜单中。

如何在 Android 上为 Notification Action 添加图标?

图标作为 drawable 资源传递给 NotificationCompat.Action.Builder。图标必须是单色的(透明通道),大小为 24x24 dp,并符合 Material Design 指南。

Notification Action 在锁屏上能工作吗?

可以,前提是通知显示在锁屏上且 visibility 标志设置为 VISIBILITY_PUBLIC。在 iOS 上,需要授权的操作需要 .authenticationRequired 选项。

如果应用被从内存中删除,如何处理操作点击?

在 Android 上,系统会重新创建进程并传递 Intent。在 iOS 上,系统会在后台启动应用。可以通过 Android 上的 BroadcastReceiver 和 iOS 上的 UNNotificationServiceExtension 来保证传递。

从用户体验的角度看,Android 和 iOS 上的操作有什么区别?

在 Android 上,操作以图标形式立即显示在通知下方。在 iOS 上,操作隐藏在长按或滑动背后。通知的 UX 设计应考虑这些平台差异。

总结

  • Notification Action — 推送通知中用于快速交互的按钮和输入框。
  • 在 Android 上,操作通过 NotificationCompat.Action 和 PendingIntent 创建。
  • 在 iOS 上,操作通过 UNNotificationAction 和分类注册定义。
  • 文本输入通过 RemoteInput (Android) 或 UNTextInputNotificationAction (iOS) 实现。
  • 最大操作数—Android 3 个,iOS 4 个。
  • 每个操作应在分析系统中记录以便优化通知。
  • 为了可靠处理,请在 Android 上使用 BroadcastReceiver,在 iOS 上使用 AppDelegate。

我们将开发一款交钥匙移动应用程序

IT Sectr自2017年以来为初创企业和企业打造iOS和Android应用程序。我们将为您提供咨询并提出最佳解决方案。

讨论项目

另请阅读