iOS SDK 高级集成指南
本节提供有关 Pushwoosh iOS SDK 高级集成的详细信息。
后台模式
Anchor link to要启用此功能,您必须将后台模式(Background Modes)添加到您的项目中。
启用后台模式的步骤
Anchor link to- 在 Xcode 中打开您的项目,并在 Project Navigator 中选择它。
- 从左侧面板中选择您的应用目标。
- 导航到 Signing & Capabilities 选项卡。
- 点击左上角的 + Capability 按钮。
- 从列表中搜索并选择 Background Modes。
- 在 Background Modes 部分,勾选 Remote notifications 以启用远程通知。
完成后,您的应用将能够在后台处理推送通知,包括静默推送。
前台模式
Anchor link to默认情况下,当应用在前台运行时,Pushwoosh iOS SDK 会显示通知横幅。
您可以通过在代码中(例如,在您的 AppDelegate 中)设置以下布尔标志来控制此行为:
// Set false to disable foreground notifications, true to enable itPushwoosh.sharedInstance().showPushnotificationAlert = true// Set 0 to disable foreground notifications, 1 to enable it[[Pushwoosh sharedInstance] setShowPushnotificationAlert:0];日志级别
Anchor link toPushwoosh iOS SDK 支持以下日志级别:
NONE- SDK 不输出任何日志。ERROR- 仅在控制台中显示错误消息。WARNING- 除错误外,还显示警告。INFO- 包含信息性消息(默认设置)。DEBUG- 包含详细的调试信息。
默认情况下,日志级别设置为 INFO,确保 SDK 提供相关信息而不会使开发者控制台混乱。
要修改日志级别,请更新应用 Info.plist 文件中的 Pushwoosh_LOG_LEVEL 键:
<key>Pushwoosh_LOG_LEVEL</key><string>YOUR_LOG_LEVEL</string>或者,您可以使用以下代码片段更改日志级别:
Pushwoosh.Debug.setLogLevel(.PW_LL_DEBUG)将 YOUR_LOG_LEVEL 替换为所需的级别(例如,DEBUG 或 ERROR)。
自定义 UNNotificationCenterDelegate
Anchor link to如果您想使用自己的 UNNotificationCenterDelegate(例如,用于本地通知),您应该告知 Pushwoosh SDK 以确保其正常运行。您可以通过 Pushwoosh 实例的 notificationCenterDelegateProxy 属性来实现:
Pushwoosh.sharedInstance()?.notificationCenterDelegateProxy.add(my_delegate)[Pushwoosh.sharedInstance.notificationCenterDelegateProxy addNotificationCenterDelegate:my_delegate];然后,在您的代理中实现 UNNotificationCenterDelegate 方法:
func userNotificationCenter( _ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) { if (!PWMessage.isPushwooshMessage(notification.request.content.userInfo)) { // Handle your notification completionHandler(UNNotificationPresentationOptions.alert) }}
func userNotificationCenter( _ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { if (!PWMessage.isPushwooshMessage(response.notification.request.content.userInfo)) { // Handle your notification completionHandler() }}- (void)userNotificationCenter:(UNNotificationCenter *)center willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler { if (![PWMessage isPushwooshMessage:notification.request.content.userInfo]) { // Handle your message completionHandler(UNNotificationPresentationOptionAlert); }}
- (void)userNotificationCenter:(UNNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler { if (![PWMessage.isPushwooshMessage:response.notification.request.content.userInfo]) { // Handle your message completionHandler(); }}Pushwoosh 延迟初始化
Anchor link toPushwoosh_LAZY_INITIALIZATION 标志可防止 Pushwoosh SDK 在应用启动时自动初始化。这允许对 Pushwoosh SDK 服务何时启动进行更多控制。
启用此标志后,Pushwoosh SDK 不会启动其服务,直到明确调用 Pushwoosh iOS SDK 方法。
将以下条目添加到 Info.plist:
<key>Pushwoosh_LAZY_INITIALIZATION</key><true/>用例
-
受控 SDK 初始化 –
Pushwoosh_LAZY_INITIALIZATION标志允许延迟 Pushwoosh SDK 的启动,从而对何时激活推送服务进行更多控制。 -
延迟推送激活 – 在某些应用中,推送通知应仅在特定条件下初始化。启用此标志可确保 Pushwoosh SDK 仅在明确请求时启动。
-
用户特定推送配置 – 某些应用可能需要根据用户偏好或账户设置自定义推送通知设置。通过延迟初始化,Pushwoosh SDK 仅在确定适当配置后才启动。
Info.plist 属性完整列表
Anchor link to| 属性 | 描述 | 可能的值 |
|---|---|---|
Pushwoosh_APPID | 设置用于生产构建的 Pushwoosh 应用 ID。 | XXXXX-XXXXX 类型:字符串 |
Pushwoosh_APPID_Dev | 设置用于开发构建的 Pushwoosh 应用 ID。 | XXXXX-XXXXX 类型:字符串 |
Pushwoosh_SHOW_ALERT | 显示通知前台警报。 | 是 (默认) / 否 类型:布尔值 |
Pushwoosh_ALERT_TYPE | 设置通知警报样式。 | BANNER (默认) / ALERT / NONE 类型:字符串 |
Pushwoosh_BASEURL | 覆盖 Pushwoosh 服务器基础 URL。 | https://cp.pushwoosh.com/json/1.3/ (默认) 类型:字符串 |
Pushwoosh_AUTO_ACCEPT_DEEP_LINK_FOR_SILENT_PUSH | 如果为 YES,静默推送中收到的 Deep Link 将自动处理。 | 是 (默认) / 否 类型:布尔值 |
Pushwoosh_ALLOW_SERVER_COMMUNICATION | 允许 SDK 向 Pushwoosh 服务器发送网络请求。 | 是 (默认) / 否 类型:布尔值 |
Pushwoosh_ALLOW_COLLECTING_DEVICE_DATA | 允许 SDK 收集并将设备数据(操作系统版本、区域设置和型号)发送到服务器。 | 是 (默认) / 否 类型:布尔值 |
Pushwoosh_ALLOW_COLLECTING_DEVICE_OS_VERSION | 允许 SDK 收集并将设备的操作系统版本发送到服务器。 | 是 (默认) / 否 类型:布尔值 |
Pushwoosh_ALLOW_COLLECTING_DEVICE_LOCALE | 允许 SDK 收集并将设备区域设置发送到服务器。 | 是 (默认) / 否 类型:布尔值 |
Pushwoosh_ALLOW_COLLECTING_DEVICE_MODEL | 允许 SDK 收集并将设备型号发送到服务器。 | 是 (默认) / 否 类型:布尔值 |
Pushwoosh_LOG_LEVEL | Pushwoosh SDK 日志级别。有关详细信息,请参阅控制日志级别。 | NONE / ERROR / WARNING / INFO (默认) / DEBUG / VERBOSE 类型:字符串 |
Pushwoosh_PURCHASE_TRACKING_ENABLED | 允许 SDK 跟踪应用内购买。Customer Journey Builder 所需。 | 是 / 否 (默认) 类型:布尔值 |