# สร้างพุชแบบโต้ตอบสำหรับ iOS

iOS 8 ได้เปิดตัวการแจ้งเตือนแบบโต้ตอบ ซึ่งช่วยให้ผู้ใช้สามารถดำเนินการได้โดยตรงจากแบนเนอร์การแจ้งเตือน ตอนนี้ Pushwoosh มี **iOS Categories** ซึ่งช่วยให้คุณสามารถสร้างปุ่มที่กำหนดเองได้ภายใน Pushwoosh Control Panel [เรียนรู้เพิ่มเติม](/th/product/messaging-channels/push-notifications/advanced-push-features/ios-interactive-push)

## API

เมื่อแอปพลิเคชันของคุณเรียกใช้ [`registerDevice`](/th/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`](/th/developer/api-reference/messages-api/#createmessage) คุณควรใช้พารามิเตอร์ `ios_category` พร้อมกับ Category ID ที่สอดคล้องกันเป็นค่า:

```json
{
  "categoryId": 65  // ไม่บังคับ ค่าสตริง ID หมวดหมู่ iOS8 จาก 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` ได้มาจาก payload ของการแจ้งเตือน

จากนั้น สร้างอินสแตนซ์ของคลาสนี้และส่งต่อไปยัง 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 พร้อมปุ่มการดำเนินการที่กำหนดเองซึ่งแสดงในแบนเนอร์การแจ้งเตือน"/>