การผสานการทำงานของ Rich Notification บน iOS
ตั้งแต่ iOS 10 เป็นต้นไป คุณสามารถแนบรูปภาพนิ่ง ภาพเคลื่อนไหว หรือแม้แต่วิดีโอไปกับการแจ้งเตือนแบบพุชได้ โดยจะแสดงผลโดยตรงในการแจ้งเตือนเมื่อผู้ใช้กดแบบ Force-tap
การเปิดใช้งานฟังก์ชันนี้ทำได้ง่ายมาก และใช้โค้ดเพียงเล็กน้อย มาเริ่มกันเลย!
1. การสร้าง Notification Service Extension
Anchor link toขั้นแรก ให้สร้าง Notification Service Extension ส่วนขยายนี้จะดาวน์โหลดเนื้อหาที่จะแสดงให้ผู้ใช้เห็น
เพิ่ม target ใหม่ในโปรเจกต์ของคุณ (File -> New -> Target) และสร้าง Notification Service Extension

2. โค้ดสำหรับ Notification Service Extension
Anchor link toโค้ดนี้จะดาวน์โหลดไฟล์แนบและเรียกใช้ตัวจัดการเนื้อหาการแจ้งเตือน (notification content handler) เพียงแค่คัดลอกและวางโค้ดนี้ลงในส่วนขยายของคุณ
import UserNotificationsimport PushwooshFramework
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)? var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) { PWNotificationExtensionManager.shared().handle(request, contentHandler: contentHandler) }}
#import "PWNotificationExtensionManager.h"
@interface NotificationService : UNNotificationServiceExtension
@end
@implementation NotificationService
- (void)didReceiveNotificationRequest:(UNNotificationRequest *)request withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler { [[PWNotificationExtensionManager sharedManager] handleNotificationRequest:request contentHandler:contentHandler];}
@end
3. การอนุญาต URL ของไฟล์แนบที่ไม่ปลอดภัย
Anchor link toNotification Service Extension เป็นไบนารีแยกต่างหากและมีไฟล์ Info.plist ของตัวเอง ให้เพิ่ม App Transport Security Settings พร้อมกับตั้งค่าแฟล็ก Allow Arbitrary Loads เป็น true
ในไฟล์ Info.plist ของส่วนขยาย
Info.plist:
<key>NSAppTransportSecurity</key><dict> <key>NSAllowsArbitraryLoads</key> <true/></dict>
4. การส่ง Rich Notification
Anchor link toหากต้องการส่ง Rich Notification เพียงระบุ URL ของไฟล์ในช่อง BANNER URL

Woosh! กดแบบ Force-tap ที่การแจ้งเตือน เท่านี้ก็เรียบร้อย!

แบ่งปันความคิดเห็นของคุณกับเรา
Anchor link toความคิดเห็นของคุณช่วยให้เราสร้างประสบการณ์ที่ดียิ่งขึ้น เราจึงยินดีรับฟังหากคุณพบปัญหาใดๆ ระหว่างขั้นตอนการผสานการทำงานของ SDK หากคุณประสบปัญหาใดๆ โปรดอย่าลังเลที่จะแบ่งปันความคิดเห็นของคุณกับเรา ผ่านแบบฟอร์มนี้