# การแจ้งเตือน push แบบกำหนดเองในเบื้องหน้าสำหรับ iOS

ตั้งแต่เวอร์ชัน 6.10.0 คุณสามารถผสานรวมโมดูล `PushwooshForegroundPush` เพื่อปรับแต่งการแจ้งเตือน push ในเบื้องหน้าเมื่อการแจ้งเตือนของระบบ iOS แบบเนทีฟถูกปิดใช้งาน

### 1. ปิดใช้งานการแจ้งเตือน push ในเบื้องหน้าแบบเนทีฟ

เพิ่ม `Pushwoosh_SHOW_ALERT = false` ไปยัง `Info.plist` ของคุณ

```xml
<key>Pushwoosh_SHOW_ALERT</key>
<false/>
```

### 2. การผสานรวมโมดูล `PushwooshForegroundPush`

**Swift Package Manager**
<img src="/spm-foreground-push-ios.webp" alt=""/>

<Aside type="caution" title="สำคัญ">
โมดูล ```PushwooshFramework```, ```PushwooshCore```, ```PushwooshBridge```, และ ```PushwooshLiveActivities``` เป็นสิ่งที่ **จำเป็น**
</Aside>

**Cocoapods**
```bash
# Uncomment the next line to define a global platform for your project
# platform :ios, '13.0'

target 'MyApp' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  pod 'PushwooshXCFramework'
  pod 'PushwooshFramework/PushwooshForegroundPush'

end
```

### 3. เพิ่มการกำหนดค่า `PushwooshForegroundPush` ใน AppDelegate

```swift
import UIKit
import PushwooshFramework
import PushwooshForegroundPush

@main
class AppDelegate: UIResponder, UIApplicationDelegate, PWMessagingDelegate, PWForegroundPushDelegate {

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        Pushwoosh.ForegroundPush.foregroundNotificationWith(style: .style1,
                                                            duration: 5,
                                                            vibration: .notification,
                                                            disappearedPushAnimation: .balls)
        
        Pushwoosh.ForegroundPush.delegate = self
        
        return true
    }

    func pushwoosh(_ pushwoosh: Pushwoosh, onMessageReceived message: PWMessage) {
        if let payload = message.payload {
          // Pushwoosh method
          Pushwoosh.ForegroundPush.showForegroundPush(userInfo: payload)
        }
    }
}
```

การใช้เมธอด `foregroundNotificationWith`

เมธอด foregroundNotificationWith ช่วยให้คุณสามารถแสดงการแจ้งเตือน push ในเบื้องหน้าแบบกำหนดเองพร้อมสไตล์ ระยะเวลา และการตอบสนองแบบสัมผัสที่กำหนดค่าได้

ลายเซ็นเมธอด (Swift / Objective-C):

```swift
@objc
static func foregroundNotificationWith(
    style: PWForegroundPushStyle,
    duration: Int,
    vibration: PWForegroundPushHapticFeedback,
    disappearedPushAnimation: PWForegroundPushDisappearedAnimation
)
```

`พารามิเตอร์:`

1. `style` (`PWForegroundPushStyle`)
* ปัจจุบันมีเพียง style1 เท่านั้นที่ใช้งานได้

2. `duration` (`Int`)
* ระบุระยะเวลาที่การแจ้งเตือนจะแสดงก่อนที่จะหายไป (เป็นวินาที)

3. `vibration` (`PWForegroundPushHapticFeedback`)
* ควบคุมการตอบสนองแบบสัมผัสเมื่อการแจ้งเตือนแสดงขึ้น ตัวเลือกที่ใช้ได้:

```swift
case none           // No vibration
case light          // Light vibration
case medium         // Medium vibration
case heavy          // Heavy vibration
case soft           // Soft vibration
case rigid          // Rigid vibration
case notification   // Standard notification vibration
```

4. `disappearedPushAnimation` (`PWForegroundPushDisappearedAnimation`)
* แอนิเมชันการหายไปของ push

```swift
case balls = 0
case regularPush
```

### 4. การใช้งานเมธอด `didTapForegroundPush` Delegate

เพื่อจัดการการแตะของผู้ใช้บนการแจ้งเตือน push ในเบื้องหน้าแบบกำหนดเอง ให้ใช้งานเมธอดโปรโตคอล `PWForegroundPushDelegate`:

```swift
// Handle tap on foreground push
func didTapForegroundPush(_ userInfo: [AnyHashable : Any]) {
    print("Foreground custom push: \(userInfo)")

    // Perform any action, e.g., navigate to a specific screen
    // navigateToScreen(for: userInfo)
}
```

หมายเหตุ:

* เมธอดนี้จะถูกเรียกเมื่อผู้ใช้แตะที่ push ในเบื้องหน้าแบบกำหนดเอง
* userInfo มี payload ของการแจ้งเตือน
* ตรวจสอบให้แน่ใจว่าได้ตั้งค่า `Pushwoosh.ForegroundPush.delegate = self` หลังจากการกำหนดค่า

### 5. พารามิเตอร์ทางเลือกสำหรับการปรับแต่งการแจ้งเตือน push ในเบื้องหน้า

โมดูล `PushwooshForegroundPush` มีพารามิเตอร์ทางเลือกหลายอย่างเพื่อปรับแต่งลักษณะและการทำงานของการแจ้งเตือน push ในเบื้องหน้าของคุณ สามารถตั้งค่าเหล่านี้ได้ทั่วโลกผ่านคุณสมบัติแบบ static

<table>
  <thead>
    <tr>
      <th style={{ width: '20%' }}>คุณสมบัติ</th>
      <th style={{ width: '15%' }}>ประเภท</th>
      <th style={{ width: '45%' }}>คำอธิบาย</th>
      <th style={{ width: '20%' }}>ค่าเริ่มต้น</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>useLiquidView</td>
      <td>Bool</td>
      <td>ใช้มุมมอง Liquid Glass บน iOS 26</td>
      <td>false</td>
    </tr>
    <tr>
      <td>gradientColors</td>
      <td>[UIColor]?</td>
      <td>อาร์เรย์ของสีที่เป็นทางเลือกสำหรับพื้นหลังแบบไล่ระดับสี</td>
      <td>nil</td>
    </tr>
    <tr>
      <td>backgroundColor</td>
      <td>UIColor?</td>
      <td>สีพื้นหลังสำหรับ push หากเป็น nil และไม่ได้ตั้งค่า gradientColors จะใช้การไล่ระดับสีเริ่มต้น</td>
      <td>Default system gradient</td>
    </tr>
    <tr>
      <td>usePushAnimation</td>
      <td>Bool</td>
      <td>จะให้มีแอนิเมชันของ push เมื่อแสดงหรือไม่</td>
      <td>true</td>
    </tr>
    <tr>
      <td>titlePushColor</td>
      <td>UIColor?</td>
      <td>สีของข้อความหัวข้อการแจ้งเตือน ค่าเริ่มต้นคือสีขาวของระบบหากเป็น nil</td>
      <td>white</td>
    </tr>
    <tr>
      <td>messagePushColor</td>
      <td>UIColor?</td>
      <td>สีของข้อความเนื้อหาการแจ้งเตือน ค่าเริ่มต้นคือสีขาวของระบบหากเป็น nil</td>
      <td>white</td>
    </tr>
    <tr>
      <td>titlePushFont</td>
      <td>UIFont?</td>
      <td>ฟอนต์ของข้อความหัวข้อการแจ้งเตือน ค่าเริ่มต้นคือฟอนต์ของระบบหากเป็น nil</td>
      <td>Default system font</td>
    </tr>
    <tr>
      <td>messagePushFont</td>
      <td>UIFont?</td>
      <td>ฟอนต์ของข้อความเนื้อหาการแจ้งเตือน ค่าเริ่มต้นคือฟอนต์ของระบบหากเป็น nil</td>
      <td>Default system font</td>
    </tr>
  </tbody>
</table>

<Aside type="caution" title="สำคัญ">
- หากเปิดใช้งานแฟล็ก `useLiquidView` แต่เวอร์ชันระบบของผู้ใช้ต่ำกว่า **iOS 26** จะแสดง push ที่ใช้ `UIView` แบบปกติแทน
- หากโปรเจกต์ของคุณคอมไพล์ด้วย Swift เวอร์ชันที่ **ต่ำกว่า 5.13** เอฟเฟกต์ Liquid Glass จะไม่สามารถใช้งานได้เลย แม้จะอยู่บน iOS 26 ก็ตาม ในกรณีนั้น จะใช้ `UIVisualEffectView` แบบเบลอ (ด้วย `UIBlurEffect`) แทนในทุกอุปกรณ์
</Aside>

**สรุป:**
- Swift 5.13+ + iOS 26 → Liquid Glass
- Swift 5.13+ + iOS < 26 → Standard UIView
- Swift < 5.13 → มุมมองแบบเบลอเสมอ (ไม่รองรับ Liquid Glass)


**ตัวอย่างการใช้งาน:**

```swift
Pushwoosh.ForegroundPush.useLiquidView = true
Pushwoosh.ForegroundPush.gradientColors = [.red, .orange, .yellow]
Pushwoosh.ForegroundPush.titlePushColor = .red
Pushwoosh.ForegroundPush.messagePushColor = .green
Pushwoosh.ForegroundPush.backgroundColor = .black
Pushwoosh.ForegroundPush.titlePushFont = .boldSystemFont(ofSize: 22)
Pushwoosh.ForegroundPush.messagePushFont = .italicSystemFont(ofSize: 15)
Pushwoosh.ForegroundPush.usePushAnimation = false
```

### 6. ตัวอย่างของ Foreground Push

ตัวอย่างนี้สาธิตวิธีการแสดงการแจ้งเตือน push ในเบื้องหน้าแบบกำหนดเองด้วย `title`, `message`, `cards`, และ `GIF animation`

<figure style={{ textAlign: "center" }}>
  <video src="/ios-foreground-custom-5.webm" title="Example" autoplay loop muted playsinline />
  <figcaption>Pushwoosh foreground push พร้อมมุมมอง Liquid Glass แบบเคลื่อนไหว</figcaption>
</figure>

<figure style={{ textAlign: "center" }}>
  <video src="/ios-foreground-custom-1.webm" title="Example" autoplay loop muted playsinline />
  <figcaption>Pushwoosh foreground push พร้อมไฟล์แนบ gif</figcaption>
</figure>

<figure style={{ textAlign: "center" }}>
  <video src="/ios-foreground-custom-2.webm" title="Example" autoplay loop muted playsinline />
  <figcaption>Pushwoosh foreground push พร้อมรูปภาพการ์ด</figcaption>
</figure>

<figure style={{ textAlign: "center" }}>
  <video src="/ios-foreground-custom-3.webm" title="Example" autoplay loop muted playsinline />
  <figcaption>Pushwoosh foreground push พร้อมการไล่ระดับสีที่กำหนดเอง และสีของหัวข้อและข้อความที่กำหนดเอง</figcaption>
</figure>

<figure style={{ textAlign: "center" }}>
  <video src="/ios-foreground-custom-4.webm" title="Example" autoplay loop muted playsinline />
  <figcaption>Pushwoosh foreground push พร้อมพื้นหลัง, ฟอนต์ของหัวข้อและข้อความที่กำหนดเอง และไม่มีแอนิเมชัน</figcaption>
</figure>

เพียงเท่านี้ คุณได้กำหนดค่าการแจ้งเตือน push ในเบื้องหน้าแบบกำหนดเองใน iOS ด้วย Pushwoosh สำเร็จแล้ว