ข้ามไปยังเนื้อหา

สร้าง Push แบบโต้ตอบสำหรับ iOS

iOS 8 ได้เปิดตัวการแจ้งเตือนแบบโต้ตอบ ซึ่งช่วยให้ผู้ใช้สามารถดำเนินการต่างๆ ได้โดยตรงจากแบนเนอร์การแจ้งเตือน ตอนนี้ Pushwoosh มี iOS Categories ซึ่งช่วยให้คุณสามารถสร้างปุ่มที่กำหนดเองได้ภายใน Pushwoosh Control Panel เรียนรู้เพิ่มเติม

เมื่อแอปพลิเคชันของคุณเรียกใช้ registerDevice Pushwoosh API จะส่งคืนการตอบสนองที่มีรายการ Categories ที่พร้อมใช้งาน พร้อมด้วย ID และรายละเอียดสำหรับแต่ละปุ่มดังนี้:

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

หากต้องการส่ง Push ของคุณพร้อมกับหมวดหมู่จาก Pushwoosh Journey เพียงเลือกหมวดหมู่นั้นในการตั้งค่าแพลตฟอร์ม iOS ขณะสร้างข้อความของคุณ ในกรณีที่คุณส่ง Push จากระยะไกลผ่าน Pushwoosh API ในคำขอ createMessage คุณควรใช้พารามิเตอร์ ios_category พร้อมกับ Category ID ที่สอดคล้องกันเป็นค่า:

{
"categoryId": 65 // ไม่บังคับ ค่าสตริง ID หมวดหมู่ iOS8 จาก Pushwoosh
}

เมื่อข้อความ Push ที่มี ID หมวดหมู่มาถึง Pushwoosh SDK จะแสดงการแจ้งเตือนพร้อมชุดปุ่มที่หมวดหมู่นี้มีอยู่

ปุ่มและการดำเนินการใน Pushwoosh iOS SDK

Anchor link to

เพื่อที่จะดำเนินการต่างๆ เมื่อเปิดแอป คุณควรสร้างการใช้งาน UNUserNotificationCenterDelegate ที่กำหนดเองและ override เมธอด didReceiveNotificationResponse ของมัน:

CustomDelegate

Anchor link to
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" {
// ทำบางอย่าง
} else {
// ทำอย่างอื่น
}
}
completionHandler()
}
}

โดยที่ identifier คือ ID ของปุ่ม และ category ได้มาจาก payload ของการแจ้งเตือน

จากนั้น สร้างอินสแตนซ์ของคลาสนี้และส่งต่อไปยัง Pushwoosh SDK โดยใช้เมธอด proxy:

AppDelegate

Anchor link to
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
Pushwoosh.sharedInstance().registerForPushNotifications()
let customDelegate = CustomDelegate()
Pushwoosh.sharedInstance().notificationCenterDelegateProxy?.add(customDelegate)
return true
}
การแจ้งเตือน Push แบบโต้ตอบของ iOS พร้อมปุ่มการดำเนินการที่กำหนดเองซึ่งแสดงในแบนเนอร์การแจ้งเตือน