انتقل إلى المحتوى

دليل التكامل الأساسي لـ 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، تأكد من إضافة الأطر الثلاثة التالية إلى هدف تطبيقك عند التكامل عبر Swift Package Manager:

  • PushwooshFramework
  • PushwooshCore
  • PushwooshBridge

افتح ملف Podfile الخاص بك وأضف التبعية:

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)، قم بتشغيل الأمر التالي لتثبيت التبعيات:

Terminal window
pod install

2. الإمكانيات (Capabilities)

Anchor link to

لتمكين الإشعارات الفورية في مشروعك، تحتاج إلى إضافة إمكانيات معينة.

في قسم Signing & Capabilities، أضف الإمكانيات التالية:

  • Push Notifications
  • Background Modes. بعد إضافة هذه الإمكانية، حدد مربع Remote notifications.

إذا كنت تنوي استخدام الإشعارات الحساسة للوقت (Time Sensitive Notifications) (iOS 15+)، فأضف أيضًا إمكانية Time Sensitive Notifications.

3. كود التهيئة

Anchor link to

AppDelegate

Anchor link to

أضف الكود التالي إلى فئة AppDelegate الخاصة بك:

import SwiftUI
import PushwooshFramework
@main
struct MyApp: App {
// Register AppDelegate as 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 {
// Initialization code
// Set custom delegate for push handling
Pushwoosh.configure.delegate = self
// Register for push notifications
Pushwoosh.configure.registerForPushNotifications()
return true
}
// Handle token received from APNS
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Pushwoosh.configure.handlePushRegistration(deviceToken)
}
// Handle token receiving error
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
Pushwoosh.configure.handlePushRegistrationFailure(error)
}
//for silent push notifications
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
Pushwoosh.configure.handlePushReceived(userInfo)
completionHandler(.noData)
}
// Fired when a push is received
func pushwoosh(_ pushwoosh: Pushwoosh, onMessageReceived message: PWMessage) {
print("onMessageReceived: ", message.payload!.description)
}
// Fired when a user taps the notification
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.
  • قم بتعيين مفتاح Pushwoosh_API_TOKEN إلى Pushwoosh Device API Token

4. تتبع تسليم الرسائل

Anchor link to

يدعم Pushwoosh تتبع أحداث التسليم للإشعارات الفورية عبر Notification Service Extension

إضافة Notification Service Extension

Anchor link to
  1. في Xcode، حدد File > New > Target…
  2. اختر Notification Service Extension واضغط على Next.
  3. أدخل اسم الهدف واضغط على Finish.
  4. عند المطالبة بالتفعيل، اضغط على Cancel.

التبعيات لـ Notification Service Extension (لـ CocoaPods فقط)

Anchor link to

ملاحظة: إذا كنت تستخدم Swift Package Manager لإدارة التبعيات، يمكنك تخطي هذه الخطوة، حيث تتم إضافة التبعيات تلقائيًا.

افتح ملف Podfile الخاص بك وأضف التبعية للهدف:

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) لتحديث التبعيات:

Terminal window
pod update

إضافة Pushwoosh SDK إلى Notification Service Extension

Anchor link to

استبدل فئة NotificationService التي تم إنشاؤها بفئة فرعية من PushwooshNotificationServiceExtension. بعد ذلك، يتولى Pushwoosh كل ما يحتاجه الإشعار الفوري — إرسال حدث التسليم، وعد الشارات (badge)، وتنزيل المرفقات الوسائط، والمهلة الإلزامية serviceExtensionTimeWillExpire. لا يلزم أي كود آخر.

import PushwooshFramework
class NotificationService: PushwooshNotificationServiceExtension {}

ملاحظة: يمكنك تخطي ملف المصدر بالكامل — قم بتعيين NSExtensionPrincipalClass الخاص بالامتداد إلى PushwooshNotificationServiceExtension في ملف Info.plist الخاص به ولا تكتب أي كود على الإطلاق.

لتعديل الإشعار قبل عرضه، قم بتجاوز didReceive(_:withContentHandler:)، واستدعِ super مع معالج المحتوى الخاص بك، وقم بتعديل المحتوى بداخله، ثم قم بتمريره إلى المعالج الأصلي. سيظل Pushwoosh يقوم بتشغيل حدث التسليم، والشارة، والمرفق، والمهلة.

import UserNotifications
import PushwooshFramework
class NotificationService: PushwooshNotificationServiceExtension {
override func didReceive(_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
super.didReceive(request) { content in
let mutable = (content.mutableCopy() as? UNMutableNotificationContent) ?? content
// Modify the notification content here...
contentHandler(mutable)
}
}
}

Info.plist

Anchor link to

يرث الامتداد Pushwoosh_APPID (ومفاتيح Pushwoosh_* الأخرى) من التطبيق المضيف، لذلك لا تحتاج إلى تكرارها في ملف Info.plist الخاص بالامتداد. أضف مفتاحًا هناك فقط عندما تريد تجاوز قيمة المضيف.

لمزامنة عدد الشارات وإعدادات الوكيل العكسي مع التطبيق، شارك App Group بين التطبيق والامتداد. أضف إمكانية App Groups إلى كلا الهدفين، ثم قم بتعيين معرف App Group في ملف Info.plist الخاص بـ التطبيق الرئيسي:

  • PW_APP_GROUPS_NAME - معرف App Group الخاص بك (على سبيل المثال، group.com.example.app).

يرث الامتداد هذه القيمة من التطبيق المضيف، لذلك لا تحتاج إلى تكرارها في ملف Info.plist الخاص بالامتداد — أضفها هناك فقط لتجاوز المضيف. بدلاً من ذلك، قم بتوفيرها في الكود عن طريق تجاوز pushwooshAppGroupsName.

5. تشغيل المشروع

Anchor link to
  1. قم ببناء وتشغيل المشروع.
  2. انتقل إلى لوحة تحكم Pushwoosh وأرسل إشعارًا فوريًا.
  3. يجب أن ترى الإشعار في التطبيق.

تكامل Pushwoosh iOS الممتد

Anchor link to

في هذه المرحلة، تكون قد قمت بالفعل بدمج SDK ويمكنك إرسال واستقبال الإشعارات الفورية. الآن، دعنا نستكشف الوظائف الأساسية

الإشعارات الفورية

Anchor link to

في Pushwoosh SDK، هناك استدعاءان (callbacks) مصممان للتعامل مع الإشعارات الفورية:

  • onMessageReceived: يتم استدعاء هذه الطريقة عند استلام إشعار فوري.
  • onMessageOpened: يتم استدعاء هذه الطريقة عندما يتفاعل المستخدم مع الإشعار (يفتحه)

تمكّن هذه الاستدعاءات المطورين من إدارة استقبال الإشعارات الفورية وتفاعل المستخدم معها داخل تطبيقاتهم

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
// set user ID
if let userId = user.userId {
pushwoosh.setUserId(userId)
}
// set user email
if let userEmail = user.email {
pushwoosh.setEmail(userEmail)
}
// set user SMS number
if let userSmsNumber = user.SmsNumber {
pushwoosh.registerSmsNumber(userSmsNumber)
}
// set user WhatsApp number
if let userWhatsAppNumber = user.WhatsAppNumber {
pushwoosh.registerSmsNumber(userWhatsAppNumber)
}
// setting additional user information as tags for 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)

Anchor link to

العلامات (Tags) هي أزواج من المفاتيح والقيم يتم تعيينها للمستخدمين أو الأجهزة، مما يسمح بالتقسيم بناءً على سمات مثل التفضيلات أو السلوك، مما يتيح إرسال رسائل مستهدفة.

import PushwooshFramework
class UpdateUser {
func afterUserUpdateProfile(user: User) {
let pushwoosh = Pushwoosh.configure
// set list of favorite categories
pushwoosh.setTags(["favorite_categories" : user.getFavoriteCategories()])
// set payment information
pushwoosh.setTags([
"is_subscribed": user.isSubscribed(),
"payment_status": user.getPaymentStatus(),
"billing_address": user.getBillingAddress()
])
}
}

الأحداث (Events)

Anchor link to

الأحداث (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
// Track purchase event
PWInAppManager.shared().postEvent("purchase", withAttributes: [
"product_id": product.getId(),
"product_name": product.getName(),
"price": product.getPrice(),
"quantity": product.getQuantity()
])
// Set 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

فشل في بناء الوحدة ‘PushwooshFramework’

Anchor link to

عند بناء مشروعك، قد تواجه خطأً مشابهًا لـ:

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. بدءًا من إصدار Pushwoosh iOS SDK 6.8.0، تم تقسيم SDK إلى عدة مكونات تتفاعل مع بعضها البعض. يحدث الخطأ عندما لا تتم إضافة جميع الأطر المطلوبة إلى مشروعك.

الحل: تأكد من إضافة جميع الأطر الأربعة المطلوبة إلى هدف تطبيقك عند التكامل عبر Swift Package Manager:

  • PushwooshFramework
  • PushwooshCore
  • PushwooshBridge
  • PushwooshLiveActivities

للتحقق من ذلك في Xcode:

  1. حدد مشروعك في Project Navigator
  2. حدد هدف تطبيقك
  3. انتقل إلى General > Frameworks, Libraries, and Embedded Content
  4. تأكد من إدراج جميع الأطر الأربعة

إذا واجهت أي مشاكل أثناء عملية التكامل، يرجى الرجوع إلى قسم الدعم والمجتمع.