# การผสานการทำงาน Rich Notifications ของ iOS

ตั้งแต่ iOS 10 เป็นต้นไป คุณสามารถแนบรูปภาพนิ่งหรือภาพเคลื่อนไหว หรือแม้แต่วิดีโอไปกับการแจ้งเตือนแบบพุชได้ ซึ่งจะแสดงผลในการแจ้งเตือนทันทีเมื่อผู้ใช้ทำการ force-tap

การเปิดใช้งานฟังก์ชันนี้ทำได้ง่ายมาก โดยมีการเขียนโค้ดเพียงเล็กน้อย มาเริ่มกันเลย!

## 1. การสร้าง Notification Service Extension

ขั้นแรกให้สร้าง [Notification Service Extension](https://developer.apple.com/reference/usernotifications/unnotificationserviceextension) ส่วนขยายนี้จะดาวน์โหลดเนื้อหาที่จะแสดงให้ผู้ใช้เห็น

เพิ่ม target ใหม่ไปยังโปรเจกต์ของคุณ (File -> New -> Target) และสร้าง _Notification Service Extension_

<img src="/ios-rich-notification-1.webp" alt="การสร้าง Notification Service Extension"/>

<Aside type="caution">
 Notification Service Extension มี Apple App ID และ Provisioning profile แยกต่างหาก!

โปรดทราบว่าส่วนขยายการแจ้งเตือนมี Bundle Id ของตัวเอง (_เช่น:_ `com.pushwoosh.example.NotificationService`) รวมถึง **Apple App ID** และ **Provisioning profile** ของตัวเอง ซึ่งต้องตั้งค่าใน Apple Developer Portal แยกต่างหาก
</Aside>

## 2. โค้ด Notification Service Extension

โค้ดนี้จะดาวน์โหลดไฟล์แนบและเรียกใช้ตัวจัดการเนื้อหาการแจ้งเตือน
เพียงแค่คัดลอกและวางลงในส่วนขยายของคุณ

<Tabs syncKey="code-example">
<TabItem label="Swift">
```swift
import UserNotifications
import 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)
    }
}
```
</TabItem>

<TabItem label="Objective-C">
```objective-c
#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
```
</TabItem>
</Tabs>

## 3. การอนุญาต URL ของไฟล์แนบที่ไม่ปลอดภัย

Notification Service Extension เป็นไบนารีแยกต่างหากและ**มีไฟล์ Info.plist ของตัวเอง**
เพิ่ม **App Transport Security Settings** พร้อมกับตั้งค่าแฟล็ก **Allow Arbitrary Loads** เป็น ```true``` ในไฟล์ **Info.plist ของส่วนขยาย**

**Info.plist**:
```xml
<key>NSAppTransportSecurity</key>
<dict>
	<key>NSAllowsArbitraryLoads</key>
	<true/>
</dict>
```

## 4. การส่ง Rich Notification

หากต้องการส่ง rich notification เพียงระบุ URL ของไฟล์ในช่อง **BANNER URL**

<img src="/ios-rich-notification-2.webp" alt=""/>

เรียบร้อย! Force-tap ที่การแจ้งเตือนก็เป็นอันเสร็จสิ้น!

<video src="/ios-rich-notification-3.webm" title="ตัวอย่าง" autoplay loop muted playsinline />

<Aside type="tip">
เรียนรู้วิธี [ทำให้วิดีโอใน iOS Rich Push Notifications ของคุณเล่นอัตโนมัติด้วย force touch](/th/developer/pushwoosh-sdk/ios-sdk/customizing-ios-sdk/#autoplay-a-video-sent-in-a-rich-notification-with-force-touch)
</Aside>

## แบ่งปันความคิดเห็นของคุณกับเรา

ความคิดเห็นของคุณช่วยให้เราสร้างประสบการณ์ที่ดียิ่งขึ้น ดังนั้นเรายินดีที่จะรับฟังจากคุณหากคุณมีปัญหาใดๆ ในระหว่างขั้นตอนการผสานการทำงาน SDK หากคุณประสบปัญหาใดๆ โปรดอย่าลังเลที่จะแบ่งปันความคิดเห็นของคุณกับเรา [ผ่านแบบฟอร์มนี้](https://docs.google.com/forms/d/e/1FAIpQLSd\_0b8jwn-V\_JmoPLIxIFYbHACCQhrzidOZV3ELywoQPXRSxw/viewform)