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

การติดตามการส่งข้อความบน iOS

มี API method ใน Pushwoosh ที่ติดตามการส่ง push notifications แอป iOS ไม่รองรับเมธอดนี้โดยตรง เนื่องจาก push notifications บน iOS ถูกจัดการโดย OS ไม่ใช่โดย Pushwoosh SDK คุณสามารถเพิ่มการติดตามการส่งได้โดยการเพิ่ม Notification Service Extension เข้าไปในโปรเจกต์ของคุณ หน้านี้จะแสดงวิธีนำการติดตามการส่งข้อความไปใช้สำหรับแอป iOS

เพิ่ม Notification Service Extension

Anchor link to
  1. ใน Xcode เลือก File > New > Target…

  2. เลือก Notification Service Extension และกด Next

หน้าจอเลือก template target ของ Xcode โดยเลือก Notification Service Extension
  1. ป้อนชื่อผลิตภัณฑ์และกด Finish
  1. กด Cancel ที่หน้าต่าง Activate scheme
หน้าต่าง Activate scheme โดยมีการไฮไลท์ที่ปุ่ม Cancel

การกดยกเลิกจะทำให้ Xcode ยังคงดีบักแอปของคุณแทนที่จะเป็น extension ที่คุณเพิ่งสร้างขึ้น หากคุณเผลอกด activate ไป คุณสามารถสลับกลับไปดีบักแอปของคุณได้ภายใน Xcode

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

Anchor link to

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

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

Podfile
target 'NotificationServiceExtension' do
use_frameworks!
pod 'PushwooshXCFramework'
end

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

Terminal window
rm -rf Podfile.lock
pod deintegrate
pod setup
pod repo update
pod install

เพิ่มโค้ดสำหรับติดตามเหตุการณ์การส่งข้อความ

Anchor link to

ทำให้ extension ของคุณเป็น subclass ของ PushwooshNotificationServiceExtension แค่ subclass ว่างๆ ก็เพียงพอแล้ว: Pushwoosh จะส่งเหตุการณ์การส่งข้อความ, ตั้งค่า badge, ดาวน์โหลดไฟล์แนบมีเดีย และจัดการ timeout fallback โดยอัตโนมัติ

แทนที่เนื้อหาที่สร้างขึ้นในไฟล์ NotificationService ของคุณ:

import UserNotifications
import PushwooshFramework
class NotificationService: PushwooshNotificationServiceExtension {}

ตั้งแต่เวอร์ชัน 7.1.0 เป็นต้นไป extension จะสืบทอด Pushwoosh_APPID (และคีย์ Pushwoosh_* อื่นๆ) จาก Info.plist ของแอปโฮสต์ ดังนั้นคุณไม่จำเป็นต้องทำซ้ำใน extension อีกต่อไป เพิ่ม Pushwoosh_APPID ใน Info.plist ของ extension เฉพาะเมื่อคุณต้องการแทนที่ค่าของโฮสต์:

NotificationService/Info.plist
<key>Pushwoosh_APPID</key>
<string>XXXXX-XXXXX</string>

App Group (สำหรับ badge และ reverse proxy)

Anchor link to

จำเป็นต้องมี App Group ที่ใช้ร่วมกันระหว่างแอปและ extension เพื่อซิงค์จำนวน badge และเพื่ออ่านการตั้งค่า reverse-proxy ที่แอปโฮสต์จัดเก็บไว้

  1. เพิ่มความสามารถ App Groups ให้กับ target ของ extension และเปิดใช้งาน group เดียวกันกับในแอปโฮสต์ นี่เป็นสิ่งจำเป็น — หากไม่มี container ที่ใช้ร่วมกัน จำนวน badge และการตั้งค่า reverse-proxy จะไม่สามารถซิงค์ได้

  2. ระบุชื่อ App Group เช่นเดียวกับ Pushwoosh_APPID extension จะสืบทอด PW_APP_GROUPS_NAME จาก Info.plist ของแอปโฮสต์ตั้งแต่เวอร์ชัน 7.1.0 ดังนั้นหากคุณได้ตั้งค่าไว้ที่นั่นสำหรับ badges แล้ว คุณไม่จำเป็นต้องเพิ่มใน extension อีก ตั้งค่าใน Info.plist ของ extension เฉพาะเพื่อแทนที่ค่าของโฮสต์ หรือระบุผ่านโปรแกรมโดยการ override pushwooshAppGroupsName

App Info.plist
<key>PW_APP_GROUPS_NAME</key>
<string>group.com.example.app</string>

ปรับแต่งการแจ้งเตือน (ทางเลือก)

Anchor link to

base class มีจุดให้ override อยู่สองสามจุด ตั้งแต่การควบคุมน้อยที่สุดไปจนถึงมากที่สุด Pushwoosh ยังคงรันเหตุการณ์การส่ง, badge, ไฟล์แนบ และ timeout fallback ในทุกกรณี

ตั้งค่า App Group ผ่านโปรแกรมแทนการใช้คีย์ใน Info.plist:

override func pushwooshAppGroupsName() -> String? {
"group.com.example.app"
}

รันการเตรียมการแบบ asynchronous ก่อนที่ Pushwoosh จะประมวลผล push — ตัวอย่างเช่น การ prefetch มีเดียของ Push Stories — โดยไม่ต้อง override didReceive มาตรฐาน เรียก completion เพียงครั้งเดียวบน main thread:

override func pushwooshPrepare(for request: UNNotificationRequest,
completion: @escaping () -> Void) {
// async work here
completion()
}

แก้ไขเนื้อหาก่อนที่จะแสดงโดยการ override didReceive เรียก super ด้วย content handler ของคุณเอง, เปลี่ยนแปลงเนื้อหาภายในนั้น, แล้วส่งต่อไปยัง handler เดิม:

override func didReceive(_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
super.didReceive(request) { content in
let mutable = (content.mutableCopy() as? UNMutableNotificationContent) ?? content
// customize `mutable` here
contentHandler(mutable)
}
}

การผสานรวมแบบเก่า (Legacy)

Anchor link to

API ระดับต่ำนี้ขับเคลื่อนการประมวลผลแบบเดียวกัน (เหตุการณ์การส่ง, badge, ไฟล์แนบ) จาก UNNotificationServiceExtension ทั่วไป:

import UserNotifications
import PushwooshFramework
class NotificationService: UNNotificationServiceExtension {
override func didReceive(_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
PWNotificationExtensionManager.sharedManager()
.handleNotificationRequest(request, contentHandler: contentHandler)
}
}

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

Anchor link to

ความคิดเห็นของคุณช่วยให้เราสร้างประสบการณ์ที่ดีขึ้น ดังนั้นเรายินดีที่จะรับฟังจากคุณหากคุณมีปัญหาใดๆ ในระหว่างกระบวนการผสานรวม SDK หากคุณประสบปัญหาใดๆ โปรดอย่าลังเลที่จะแบ่งปันความคิดเห็นของคุณกับเรา ผ่านแบบฟอร์มนี้