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

คู่มือการผสานการทำงานพื้นฐาน iOS SDK 7.0+

ส่วนนี้มีข้อมูลเกี่ยวกับวิธีการผสานการทำงาน Pushwoosh SDK เข้ากับแอปพลิเคชัน iOS ของคุณ

ข้อกำหนดเบื้องต้น

Anchor link to

ในการผสานการทำงาน Pushwoosh iOS SDK เข้ากับแอปของคุณ คุณจะต้องมีสิ่งต่อไปนี้:

ขั้นตอนการผสานการทำงาน

Anchor link to

1. การติดตั้ง

Anchor link to

คุณสามารถผสานการทำงาน Pushwoosh SDK เข้ากับแอปพลิเคชันของคุณได้โดยใช้ Swift Package Manager หรือ CocoaPods

Swift Package Manager

Anchor link to

ในส่วน Package Dependencies ให้เพิ่มแพ็คเกจต่อไปนี้:

https://github.com/Pushwoosh/Pushwoosh-XCFramework

ในการใช้ Pushwoosh iOS SDK ตรวจสอบให้แน่ใจว่าได้เพิ่มเฟรมเวิร์กสามตัวต่อไปนี้ไปยัง target ของแอปของคุณเมื่อทำการผสานการทำงานผ่าน Swift Package Manager:

  • PushwooshFramework
  • PushwooshCore
  • PushwooshBridge

เปิด Podfile ของคุณและเพิ่ม dependency:

Terminal window
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'MyApp' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
pod 'PushwooshXCFramework'
end

จากนั้นใน terminal ให้รันคำสั่งต่อไปนี้เพื่อติดตั้ง dependency:

Terminal window
pod install

2. Capabilities

Anchor link to

ในการเปิดใช้งาน Push Notifications ในโปรเจกต์ของคุณ คุณต้องเพิ่ม capabilities บางอย่าง

ในส่วน Signing & Capabilities ให้เพิ่ม capabilities ต่อไปนี้:

  • Push Notifications
  • Background Modes หลังจากเพิ่ม capability นี้แล้ว ให้เลือกช่องสำหรับ Remote notifications

หากคุณต้องการใช้ Time Sensitive Notifications (iOS 15+) ให้เพิ่ม capability Time Sensitive Notifications ด้วย

3. โค้ดการเริ่มต้น

Anchor link to

AppDelegate

Anchor link to

เพิ่มโค้ดต่อไปนี้ลงในคลาส AppDelegate ของคุณ:

import SwiftUI
import PushwooshFramework
@main
struct MyApp: App {
// ลงทะเบียน AppDelegate เป็น UIApplicationDelegate
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
class AppDelegate: NSObject, UIApplicationDelegate, PWMessagingDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// โค้ดการเริ่มต้น
// ตั้งค่า delegate ที่กำหนดเองสำหรับการจัดการ push
Pushwoosh.configure.delegate = self
// ลงทะเบียนสำหรับ push notifications
Pushwoosh.configure.registerForPushNotifications()
return true
}
// จัดการ token ที่ได้รับจาก APNS
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Pushwoosh.configure.handlePushRegistration(deviceToken)
}
// จัดการข้อผิดพลาดในการรับ token
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
Pushwoosh.configure.handlePushRegistrationFailure(error)
}
//สำหรับ silent push notifications
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
Pushwoosh.configure.handlePushReceived(userInfo)
completionHandler(.noData)
}
// ทำงานเมื่อได้รับ push
func pushwoosh(_ pushwoosh: Pushwoosh, onMessageReceived message: PWMessage) {
print("onMessageReceived: ", message.payload!.description)
}
// ทำงานเมื่อผู้ใช้แตะที่การแจ้งเตือน
func pushwoosh(_ pushwoosh: Pushwoosh, onMessageOpened message: PWMessage) {
print("onMessageOpened: ", message.payload!.description)
}
}
struct ContentView: View {
var body: some View {
Text("Pushwoosh with SwiftUI")
.padding()
}
}

Info.plist

Anchor link to

ใน Info.plist ของคุณ:

  • ตั้งค่าคีย์ Pushwoosh_APPID เป็น Pushwoosh Application Code ของคุณ
  • ตั้งค่าคีย์ Pushwoosh_API_TOKEN เป็น Pushwoosh Device API Token

4. การติดตามการส่งข้อความ

Anchor link to

Pushwoosh รองรับการติดตาม event การส่งสำหรับ push notifications ผ่าน Notification Service Extension

เพิ่ม Notification Service Extension

Anchor link to
  1. ใน Xcode เลือก File > New > Target…
  2. เลือก Notification Service Extension และกด Next
  3. ป้อนชื่อ target และกด Finish
  4. เมื่อได้รับแจ้งให้เปิดใช้งาน ให้กด Cancel

Dependencies สำหรับ Notification Service Extension (สำหรับ CocoaPods เท่านั้น)

Anchor link to

หมายเหตุ: หากคุณใช้ Swift Package Manager เพื่อจัดการ dependencies คุณสามารถข้ามขั้นตอนนี้ได้ เนื่องจาก dependencies จะถูกเพิ่มโดยอัตโนมัติ

เปิด Podfile ของคุณและเพิ่ม dependency สำหรับ target:

Podfile
# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'
target 'MyApp' do
# Comment the next line if you don't want to use dynamic frameworks
use_frameworks!
pod 'PushwooshXCFramework'
end
target 'MyAppNotificationExtension' do
use_frameworks!
pod 'PushwooshXCFramework'
end

รันคำสั่งต่อไปนี้ใน terminal เพื่ออัปเดต dependencies:

Terminal window
pod update

เพิ่ม Pushwoosh SDK ไปยัง Notification Service Extension

Anchor link to

โค้ดนี้ช่วยให้คุณสามารถดักจับและประมวลผลการแจ้งเตือนภายใน notification extension ของคุณได้

import UserNotifications
import PushwooshFramework
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest, withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
// Pushwoosh **********
PWNotificationExtensionManager.shared().handle(request, contentHandler: contentHandler)
// ********************
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
if let bestAttemptContent = bestAttemptContent {
// แก้ไขเนื้อหาการแจ้งเตือนที่นี่...
contentHandler(bestAttemptContent)
}
}
override func serviceExtensionTimeWillExpire() {
// ถูกเรียกก่อนที่ extension จะถูกยุติโดยระบบ
// ใช้โอกาสนี้ในการส่งเนื้อหาที่แก้ไข "ดีที่สุด" ของคุณ มิฉะนั้น payload ของ push เดิมจะถูกใช้
if let contentHandler = contentHandler, let bestAttemptContent = bestAttemptContent {
contentHandler(bestAttemptContent)
}
}
}

Info.plist

Anchor link to

ใน Info.plist ของ Notification Service Extension ของคุณ ให้เพิ่ม:

  • Pushwoosh_APPID - Application Code ของคุณ

5. รันโปรเจกต์

Anchor link to
  1. Build และรันโปรเจกต์
  2. ไปที่ Pushwoosh Control Panel และ ส่ง push notification
  3. คุณควรจะเห็นการแจ้งเตือนในแอป

การผสานการทำงาน Pushwoosh iOS แบบขยาย

Anchor link to

ในขั้นตอนนี้ คุณได้ผสานการทำงาน SDK และสามารถส่งและรับ push notifications ได้แล้ว ตอนนี้เรามาดูฟังก์ชันการทำงานหลักกัน

Push notifications

Anchor link to

ใน Pushwoosh SDK มี callbacks สองตัวที่ออกแบบมาเพื่อจัดการ push notifications:

  • onMessageReceived: เมธอดนี้จะถูกเรียกเมื่อได้รับ push notification
  • onMessageOpened: เมธอดนี้จะถูกเรียกเมื่อผู้ใช้โต้ตอบกับ (เปิด) การแจ้งเตือน

callbacks เหล่านี้ช่วยให้นักพัฒนาสามารถจัดการการรับและการโต้ตอบของผู้ใช้กับ push notifications ภายในแอปพลิเคชันของตนได้

import PushwooshFramework
class AppDelegate: NSObject, UIApplicationDelegate, PWMessagingDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
Pushwoosh.configure.delegate = self;
}
func pushwoosh(_ pushwoosh: Pushwoosh, onMessageOpened message: PWMessage) {
if let payload = message.payload {
print("onMessageOpened: \(payload)")
}
}
func pushwoosh(_ pushwoosh: Pushwoosh, onMessageReceived message: PWMessage) {
if let payload = message.payload {
print("onMessageReceived: \(payload)")
}
}
}

การกำหนดค่าผู้ใช้

Anchor link to

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

import PushwooshFramework
class Registration {
func afterUserLogin(user: User) {
let pushwoosh = Pushwoosh.configure
// ตั้งค่า user ID
if let userId = user.userId {
pushwoosh.setUserId(userId)
}
// ตั้งค่า email ผู้ใช้
if let userEmail = user.email {
pushwoosh.setEmail(userEmail)
}
// ตั้งค่าหมายเลข SMS ของผู้ใช้
if let userSmsNumber = user.SmsNumber {
pushwoosh.registerSmsNumber(userSmsNumber)
}
// ตั้งค่าหมายเลข WhatsApp ของผู้ใช้
if let userWhatsAppNumber = user.WhatsAppNumber {
pushwoosh.registerSmsNumber(userWhatsAppNumber)
}
// การตั้งค่าข้อมูลผู้ใช้เพิ่มเติมเป็น tag สำหรับ Pushwoosh
if let age = user.userDetails.age,
let name = user.userDetails.userName,
let lastLogin = user.userDetails.lastLoginDate {
pushwoosh.setTags([
"age": age,
"name": name,
"last_login": lastLogin
])
}
}
}

Tags คือคู่ key-value ที่กำหนดให้กับผู้ใช้หรืออุปกรณ์ ช่วยให้สามารถแบ่งกลุ่มตามคุณลักษณะต่างๆ เช่น ความชอบหรือพฤติกรรม ทำให้สามารถส่งข้อความแบบกำหนดเป้าหมายได้

import PushwooshFramework
class UpdateUser {
func afterUserUpdateProfile(user: User) {
let pushwoosh = Pushwoosh.configure
// ตั้งค่ารายการหมวดหมู่ที่ชื่นชอบ
pushwoosh.setTags(["favorite_categories" : user.getFavoriteCategories()])
// ตั้งค่าข้อมูลการชำระเงิน
pushwoosh.setTags([
"is_subscribed": user.isSubscribed(),
"payment_status": user.getPaymentStatus(),
"billing_address": user.getBillingAddress()
])
}
}

Events คือการกระทำหรือเหตุการณ์ที่เกิดขึ้นของผู้ใช้ภายในแอป ซึ่งสามารถติดตามเพื่อวิเคราะห์พฤติกรรมและกระตุ้นให้เกิดข้อความหรือการกระทำที่สอดคล้องกัน

import PushwooshFramework
class Registration {
func afterUserLogin(user: User) {
if let userName = user.getUserName(), let lastLogin = user.getLastLoginDate() {
PWInAppManager.shared().postEvent("login", withAttributes: [
"name": userName,
"last_login": lastLogin
])
}
}
func afterUserPurchase(user: User, product: Product) {
let pushwoosh = Pushwoosh.configure
// ติดตาม event การซื้อ
PWInAppManager.shared().postEvent("purchase", withAttributes: [
"product_id": product.getId(),
"product_name": product.getName(),
"price": product.getPrice(),
"quantity": product.getQuantity()
])
// ตั้งค่า user tags
let lastPurchaseDate = Date().timeIntervalSince1970
let lifetimeSpend = getCurrentLifetimeSpend() + product.getPrice()
pushwoosh.setTags([
"last_purchase_date": lastPurchaseDate,
"lifetime_spend": lifetimeSpend
])
}
}

Rich Media

Anchor link to

Rich media หมายถึงเนื้อหาเชิงโต้ตอบและมัลติมีเดีย เช่น รูปภาพ วิดีโอ หรือ HTML ที่ใช้ในการแจ้งเตือนและข้อความในแอปเพื่อเพิ่มการมีส่วนร่วมของผู้ใช้

import PushwooshFramework
class ViewController: UIViewController, PWRichMediaPresentingDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let richMediaConfiguration = PWModalWindowConfiguration.shared()
PWRichMediaManager.shared().delegate = self
richMediaConfiguration.configureModalWindow(with: .PWModalWindowPositionBottom,
present: .PWAnimationPresentFromBottom,
dismiss: .PWAnimationDismissDown)
}
func richMediaManager(_ richMediaManager: PWRichMediaManager!, shouldPresent richMedia: PWRichMedia!) -> Bool {
print("Rich media will be presented with: \(richMedia.pushPayload!)")
return true
}
func richMediaManager(_ richMediaManager: PWRichMediaManager!, didPresent richMedia: PWRichMedia!) {
print("Rich media has been presented with: \(richMedia.pushPayload!)")
}
func richMediaManager(_ richMediaManager: PWRichMediaManager!, didClose richMedia: PWRichMedia!) {
print("Rich media has been closed with: \(richMedia.pushPayload!)")
}
func richMediaManager(_ richMediaManager: PWRichMediaManager!, presentingDidFailFor richMedia: PWRichMedia!, withError error: (any Error)!) {
print("Failed to present rich media with: \(richMedia.pushPayload!). Error: \(error.localizedDescription)")
}
}

การแก้ไขปัญหา

Anchor link to

ไม่สามารถ build โมดูล ‘PushwooshFramework’ ได้

Anchor link to

เมื่อ build โปรเจกต์ของคุณ คุณอาจพบข้อผิดพลาดที่คล้ายกับ:

Failed to build module 'PushwooshFramework'; this SDK is not supported by the compiler
(the SDK is built with 'Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)',
while this compiler is 'Apple Swift version 6.1.2 effective-5.10 (swiftlang-6.1.2.1.2 clang-1700.0.13.5)')

สาเหตุ: ข้อผิดพลาดนี้ไม่เกี่ยวข้องกับความไม่เข้ากันของเวอร์ชัน Swift compiler ตั้งแต่ Pushwoosh iOS SDK เวอร์ชัน 6.8.0 เป็นต้นไป SDK จะถูกแบ่งออกเป็นส่วนประกอบหลายส่วนที่โต้ตอบกัน ข้อผิดพลาดเกิดขึ้นเมื่อไม่ได้เพิ่มเฟรมเวิร์กที่จำเป็นทั้งหมดลงในโปรเจกต์ของคุณ

วิธีแก้ไข: ตรวจสอบให้แน่ใจว่าได้เพิ่มเฟรมเวิร์กที่จำเป็นทั้งสี่ตัวไปยัง target ของแอปของคุณเมื่อทำการผสานการทำงานผ่าน Swift Package Manager:

  • PushwooshFramework
  • PushwooshCore
  • PushwooshBridge
  • PushwooshLiveActivities

ในการตรวจสอบสิ่งนี้ใน Xcode:

  1. เลือกโปรเจกต์ของคุณใน Project Navigator
  2. เลือก target ของแอปของคุณ
  3. ไปที่ General > Frameworks, Libraries, and Embedded Content
  4. ยืนยันว่ามีเฟรมเวิร์กทั้งสี่รายการอยู่

หากคุณพบปัญหาใดๆ ในระหว่างกระบวนการผสานการทำงาน โปรดอ้างอิงถึงส่วน การสนับสนุนและชุมชน