# 创建 iOS 交互式推送

iOS 8 引入了交互式通知，允许用户直接从通知横幅中执行操作。Pushwoosh 现在提供 **iOS Categories**，使您能够在 Pushwoosh 控制面板中创建自定义按钮。[了解更多](/zh/product/messaging-channels/push-notifications/advanced-push-features/ios-interactive-push)

## API

当您的应用程序调用 [`registerDevice`](/zh/developer/api-reference/device-api/#registerdevice) 时，Pushwoosh API 会返回一个响应，其中包含可用类别 (Categories) 的列表及其 ID 和每个按钮的详细信息，如下所示：

```json
{
  "status_code": 200,
  "status_message": "OK",
  "response": {
    "iosCategories": [
      {
        "categoryId": 65,
        "buttons": [
          {
            "id": 0,
            "label": "Rate",
            "type": "1",
            "startApplication": 1
          },
          {
            "id": 1,
            "label": "Later",
            "type": "0",
            "startApplication": 0
          }
        ]
      }
    ]
  }
}
```

这些类别 (Categories) 现在在设备上可用，因此当消息到达且您的应用程序未在前台运行时，它们可以被正确显示。

要从 Pushwoosh Journey 发送带有类别的推送，只需在撰写消息时在 iOS 平台设置中选择它即可。如果您通过 Pushwoosh API 远程发送推送，则应在 [`createMessage`](/zh/developer/api-reference/messages-api/#createmessage) 请求中使用 `ios_category` 参数，并以相应的类别 ID (Category ID) 作为其值：

```json
{
  "categoryId": 65  // Optional. String value. iOS8 category ID from Pushwoosh
}
```

当包含类别 ID 的推送消息到达时，Pushwoosh SDK 会显示通知以及该类别包含的一组按钮。

## Pushwoosh iOS SDK 中的按钮和操作

为了在打开应用时执行各种操作，您应该创建一个自定义的 `UNUserNotificationCenterDelegate` 实现并重写其 `didReceiveNotificationResponse` 方法：

#### CustomDelegate


```swift
class CustomDelegate: NSObject, UNUserNotificationCenterDelegate {
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
        let identifier = response.actionIdentifier
        let category = response.notification.request.content.categoryIdentifier

        if category == "10212" {
            if identifier == "1" {
                // DO SOMETHING
            } else {
                // DO SOMETHING ELSE
            }
        }

        completionHandler()
    }
}
```

其中 `identifier` 是按钮 ID，`category` 则从通知负载中派生。

然后，创建这个类的一个实例，并使用代理方法将其传递给 Pushwoosh SDK：

#### AppDelegate

```swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    Pushwoosh.configure.registerForPushNotifications()

    let customDelegate = CustomDelegate()
    Pushwoosh.configure.addNotificationCenterDelegate(customDelegate)

    return true
}
```

<img src="/messaging-channels-ios-interactive-push-1.png" alt="在通知横幅中显示的带有自定义操作按钮的 iOS 交互式推送通知"/>