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

Push Notification แบบภาพหมุนสำหรับ iOS

โปรโตคอล UNNotificationContentExtension เป็นจุดเริ่มต้นสำหรับส่วนขยายเนื้อหาการแจ้งเตือน (notification content app extension) ซึ่งจะแสดงอินเทอร์เฟซที่กำหนดเองสำหรับการแจ้งเตือนของแอปคุณ

1. เพิ่ม Notification Content Extension

Anchor link to

ใน Xcode เลือก File > New > Target…

เลือก Notification Content Extension

ตั้งชื่อเป็น NotificationContentExtension

2. เพิ่มโค้ดลงในแอปของคุณ

Anchor link to

ดาวน์โหลด PWNotificationExtension จาก Github และแทนที่ PWNotificationContentExtension ในโปรเจกต์ Xcode ของคุณด้วยไฟล์เดียวกันจาก Github

3. หมวดหมู่การแจ้งเตือน (Notification category)

Anchor link to

เพิ่มโค้ดด้านล่างลงในไฟล์ AppDelegate.swift ของคุณ

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
if #available(iOS 10.0, *) {
let options: UNAuthorizationOptions = [.alert]
UNUserNotificationCenter.current().requestAuthorization(options: options) { (authorized, error) in
if authorized {
let categoryIdentifier = "PWNotificationCarousel"
let carouselNext = UNNotificationAction(identifier: "PWNotificationCarousel.next", title: "➡️ RIGHT", options: [])
let carouselPrevious = UNNotificationAction(identifier: "PWNotificationCarousel.previous", title: "⬅️ LEFT", options: [])
let carouselCategory = UNNotificationCategory(identifier: categoryIdentifier, actions: [carouselNext, carouselPrevious], intentIdentifiers: [], options: [])
UNUserNotificationCenter.current().setNotificationCategories([carouselCategory])
}
}
}
return true
}

4. ส่ง Push Notification

Anchor link to

หมวดหมู่ iOS (iOS Category):

ส่ง Push Notification พร้อมกับ APS json:

APS JSON
{
"aps":{
"category":"PWNotificationCarousel"
}
}

ข้อมูลที่กำหนดเอง (Custom Data)

คุณต้องระบุ URL ของรูปภาพโดยคั่นด้วยเครื่องหมายจุลภาค ,

Example
{
"images":"image1.jpg, image2.jpg, image3.jpg"
}

เมื่อคุณได้รับการแจ้งเตือน คุณจะต้องกดค้าง (long press) หรือปัดไปทางซ้ายแล้วคลิก “ดู” (View) เพื่อขยายการแจ้งเตือน ทั้งนี้ขึ้นอยู่กับเวอร์ชันของ iOS