การแจ้งเตือน 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 toSwift Package Manager

Cocoapods
# 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'
end3. เพิ่มการกำหนดค่า PushwooshForegroundPush ใน AppDelegate
Anchor link toimport UIKitimport PushwooshFrameworkimport PushwooshForegroundPush
@mainclass 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):
@objcstatic func foregroundNotificationWith( style: PWForegroundPushStyle, duration: Int, vibration: PWForegroundPushHapticFeedback, disappearedPushAnimation: PWForegroundPushDisappearedAnimation)พารามิเตอร์:
style(PWForegroundPushStyle)
- ปัจจุบันมีเพียง style1 เท่านั้นที่ใช้งานได้
duration(Int)
- ระบุระยะเวลาที่การแจ้งเตือนจะแสดงก่อนที่จะหายไป (เป็นวินาที)
vibration(PWForegroundPushHapticFeedback)
- ควบคุมการตอบสนองแบบสัมผัสเมื่อการแจ้งเตือนแสดงขึ้น ตัวเลือกที่ใช้ได้:
case none // No vibrationcase light // Light vibrationcase medium // Medium vibrationcase heavy // Heavy vibrationcase soft // Soft vibrationcase rigid // Rigid vibrationcase notification // Standard notification vibrationdisappearedPushAnimation(PWForegroundPushDisappearedAnimation)
- แอนิเมชันการหายไปของ push
case balls = 0case regularPush4. การใช้งานเมธอด didTapForegroundPush Delegate
Anchor link toเพื่อจัดการการแตะของผู้ใช้บนการแจ้งเตือน push ในเบื้องหน้าแบบกำหนดเอง ให้ใช้งานเมธอดโปรโตคอล PWForegroundPushDelegate:
// Handle tap on foreground pushfunc 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
| คุณสมบัติ | ประเภท | คำอธิบาย | ค่าเริ่มต้น |
|---|---|---|---|
| useLiquidView | Bool | ใช้มุมมอง Liquid Glass บน iOS 26 | false |
| gradientColors | [UIColor]? | อาร์เรย์ของสีที่เป็นทางเลือกสำหรับพื้นหลังแบบไล่ระดับสี | nil |
| backgroundColor | UIColor? | สีพื้นหลังสำหรับ push หากเป็น nil และไม่ได้ตั้งค่า gradientColors จะใช้การไล่ระดับสีเริ่มต้น | Default system gradient |
| usePushAnimation | Bool | จะให้มีแอนิเมชันของ push เมื่อแสดงหรือไม่ | true |
| titlePushColor | UIColor? | สีของข้อความหัวข้อการแจ้งเตือน ค่าเริ่มต้นคือสีขาวของระบบหากเป็น nil | white |
| messagePushColor | UIColor? | สีของข้อความเนื้อหาการแจ้งเตือน ค่าเริ่มต้นคือสีขาวของระบบหากเป็น nil | white |
| titlePushFont | UIFont? | ฟอนต์ของข้อความหัวข้อการแจ้งเตือน ค่าเริ่มต้นคือฟอนต์ของระบบหากเป็น nil | Default system font |
| messagePushFont | UIFont? | ฟอนต์ของข้อความเนื้อหาการแจ้งเตือน ค่าเริ่มต้นคือฟอนต์ของระบบหากเป็น nil | Default system font |
สรุป:
- Swift 5.13+ + iOS 26 → Liquid Glass
- Swift 5.13+ + iOS < 26 → Standard UIView
- Swift < 5.13 → มุมมองแบบเบลอเสมอ (ไม่รองรับ Liquid Glass)
ตัวอย่างการใช้งาน:
Pushwoosh.ForegroundPush.useLiquidView = truePushwoosh.ForegroundPush.gradientColors = [.red, .orange, .yellow]Pushwoosh.ForegroundPush.titlePushColor = .redPushwoosh.ForegroundPush.messagePushColor = .greenPushwoosh.ForegroundPush.backgroundColor = .blackPushwoosh.ForegroundPush.titlePushFont = .boldSystemFont(ofSize: 22)Pushwoosh.ForegroundPush.messagePushFont = .italicSystemFont(ofSize: 15)Pushwoosh.ForegroundPush.usePushAnimation = false6. ตัวอย่างของ Foreground Push
Anchor link toตัวอย่างนี้สาธิตวิธีการแสดงการแจ้งเตือน push ในเบื้องหน้าแบบกำหนดเองด้วย title, message, cards, และ GIF animation
เพียงเท่านี้ คุณได้กำหนดค่าการแจ้งเตือน push ในเบื้องหน้าแบบกำหนดเองใน iOS ด้วย Pushwoosh สำเร็จแล้ว