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

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

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

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

Anchor link to

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

<key>Pushwoosh_SHOW_ALERT</key>
<false/>

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

Anchor link to

Swift Package Manager

Cocoapods

Terminal window
# 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

Anchor link to
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):

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

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

  1. style (PWForegroundPushStyle)
  • ปัจจุบันมีเพียง style1 เท่านั้นที่ใช้งานได้
  1. duration (Int)
  • ระบุระยะเวลาที่การแจ้งเตือนจะแสดงก่อนที่จะหายไป (เป็นวินาที)
  1. vibration (PWForegroundPushHapticFeedback)
  • ควบคุมการตอบสนองแบบสัมผัสเมื่อการแจ้งเตือนแสดงขึ้น ตัวเลือกที่ใช้ได้:
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
  1. disappearedPushAnimation (PWForegroundPushDisappearedAnimation)
  • แอนิเมชันการหายไปของ push
case balls = 0
case regularPush

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

Anchor link to

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

// 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 ในเบื้องหน้า

Anchor link to

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

คุณสมบัติประเภทคำอธิบายค่าเริ่มต้น
useLiquidViewBoolใช้มุมมอง Liquid Glass บน iOS 26false
gradientColors[UIColor]?อาร์เรย์ของสีที่เป็นทางเลือกสำหรับพื้นหลังแบบไล่ระดับสีnil
backgroundColorUIColor?สีพื้นหลังสำหรับ push หากเป็น nil และไม่ได้ตั้งค่า gradientColors จะใช้การไล่ระดับสีเริ่มต้นDefault system gradient
usePushAnimationBoolจะให้มีแอนิเมชันของ push เมื่อแสดงหรือไม่true
titlePushColorUIColor?สีของข้อความหัวข้อการแจ้งเตือน ค่าเริ่มต้นคือสีขาวของระบบหากเป็น nilwhite
messagePushColorUIColor?สีของข้อความเนื้อหาการแจ้งเตือน ค่าเริ่มต้นคือสีขาวของระบบหากเป็น nilwhite
titlePushFontUIFont?ฟอนต์ของข้อความหัวข้อการแจ้งเตือน ค่าเริ่มต้นคือฟอนต์ของระบบหากเป็น nilDefault system font
messagePushFontUIFont?ฟอนต์ของข้อความเนื้อหาการแจ้งเตือน ค่าเริ่มต้นคือฟอนต์ของระบบหากเป็น nilDefault system font

สรุป:

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

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

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

Anchor link to

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

Pushwoosh foreground push พร้อมมุมมอง Liquid Glass แบบเคลื่อนไหว
Pushwoosh foreground push พร้อมไฟล์แนบ gif
Pushwoosh foreground push พร้อมรูปภาพการ์ด
Pushwoosh foreground push พร้อมการไล่ระดับสีที่กำหนดเอง และสีของหัวข้อและข้อความที่กำหนดเอง
Pushwoosh foreground push พร้อมพื้นหลัง, ฟอนต์ของหัวข้อและข้อความที่กำหนดเอง และไม่มีแอนิเมชัน

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