跳到内容

自定义 iOS SDK

深层链接

Anchor link to

在您的 Info.plist 文件中,添加带有 URL IdentifierURL SchemeURL types 数组。
在下面的示例中,URL Schemecom.pushwooshURL Identifierpromotion

在您的 App Delegate 文件(通常对于 iOS 12 及更低版本是 AppDelegate.m,对于 iOS 13 及更高版本是 SceneDelegate.m)中,添加如下示例所示的相应 openURL 委托函数。该示例会检查正确的页面,从 URL 中解析 “id” 值,并打开 PromoPageViewController 作为响应。

AppDelegate.swift

func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any] = [:]) -> Bool {
let components = URLComponents(url: url, resolvingAgainstBaseURL: false)
let page = components?.host
var promotionId: String?
if page == "promotion" {
return
}
let items = components?.queryItems ?? []
for item in items {
if item.name == "id" {
promotionId = item.value
}
}
//show PromoPageViewController
}

SceneDelegate.swift

func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
guard let urlContext = URLContexts.first else { return }
let components = URLComponents(url: urlContext.url, resolvingAgainstBaseURL: false)
let page = components?.host
var promotionId: String?
guard page == "promotion" else {
return
}
let items = components?.queryItems ?? []
for item in items {
if item.name == "id" {
promotionId = item.value
}
}
//show PromoPageViewController
}

应用内购买跟踪

Anchor link to

默认情况下,应用内购买跟踪是禁用的。如果您想在配置 Customer Journeys 时跟踪应用内购买,请在 info.plist 文件中将 Pushwoosh_PURCHASE_TRACKING_ENABLED 标志设置为 true。您可以在表格中找到可用标志的列表。

如果您想手动跟踪应用内购买,可以使用下面的代码。

paymentQueue:updatedTransactions: delegate 方法中调用 PushManagersendSKPaymentTransactions 方法

func paymentQueue(_ queue: SKPaymentQueue, updatedTransactions transactions: [SKPaymentTransaction]) {
// In-Apps Tracking Pushwoosh code here
Pushwoosh.sharedInstance().sendSKPaymentTransactions(transactions)
// the rest of the code, consume transactions, etc
}
InAppTrackingViewController.swift

地理区域推送通知

Anchor link to

地理区域推送通知被封装在一个单独的框架 PushwooshGeozones 中。

  1. 将 PushwooshGeozones.framework 添加到您的项目中

要使用依赖管理器将 PushwooshGeozones.framework 添加到您的项目中,请将以下几行放入您的 podfilecartfile 中:

pod 'PushwooshXCFramework/Geozones'

或者,您可以简单地将该框架拖放到项目 Build Phases 中的 Link Binaries With Libraries 中。

  1. 将以下键添加到您的 Info.plist 中:
  • NSLocationWhenInUseUsageDescription(必需) 应用程序仅在前台运行时跟踪地理区域。
  • NSLocationAlwaysAndWhenInUseUsageDescription(必需) 应用程序在前台和后台跟踪地理区域,并显示权限请求对话框弹出窗口。
  • NSLocationAlwaysUsageDescription(可选) 应用程序始终跟踪地理区域;如果您的应用程序面向 iOS 10 及更早版本,则应使用此项。
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>for app to track Geozones in both conditions and to show a permission request dialog</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>for app to track Geozones only while running in the foreground</string>

  1. 导入框架
import PushwooshGeozones
  1. 开始地理区域跟踪
PWGeozonesManager.shared()?.startLocationTracking()

GitHub 参考

override func viewDidLoad() {
super.viewDidLoad()
// Start Geozones tracking when needed
PWGeozonesManager.shared().startLocationTracking()
}

创建富媒体队列

Anchor link to

如果需要同时显示多个富媒体页面(例如,两个或多个应用内消息的触发事件同时发生,或者在另一个触发事件发生时已经显示了一个富媒体页面),您可以设置一个富媒体页面显示队列。要创建队列,请按照以下步骤操作。

  1. 创建一个实现 PWRichMediaPresentingDelegate 的类:
@interface ChainedRichMediaPresentingDelegate () <PWRichMediaPresentingDelegate>
@property (nonatomic) NSMutableArray *queue;
@property (nonatomic) BOOL inAppIsPresenting;
@end
@implementation ChainedRichMediaPresentingDelegate
- (instancetype)init {
self = [super init];
if (self) {
_queue = [NSMutableArray new];
}
return self;
}
- (BOOL)richMediaManager:(PWRichMediaManager *)richMediaManager shouldPresentRichMedia:(PWRichMedia *)richMedia {
[_queue addObject:richMedia];
return !_inAppIsPresenting;
}
- (void)richMediaManager:(PWRichMediaManager *)richMediaManager didPresentRichMedia:(PWRichMedia *)richMedia {
_inAppIsPresenting = YES;
}
- (void)richMediaManager:(PWRichMediaManager *)richMediaManager didCloseRichMedia:(PWRichMedia *)richMedia {
_inAppIsPresenting = NO;
[_queue removeObject:richMedia];
if (_queue.count) {
[[PWRichMediaManager sharedManager] presentRichMedia:_queue.firstObject];
}
}
- (void)richMediaManager:(PWRichMediaManager *)richMediaManager presentingDidFailForRichMedia:(PWRichMedia *)richMedia withError:(NSError *)error {
[self richMediaManager:richMediaManager didCloseRichMedia:richMedia];
}
@end

2. 设置委托:

[PWRichMediaManager sharedManager].delegate = [ChainedRichMediaPresentingDelegate new];

使用压力触控自动播放富通知中发送的视频

Anchor link to

要使作为富通知附件发送的视频在通知展开时无需任何用户交互即可自动播放,请按照以下步骤操作:

  1. 将通知内容扩展添加到您的项目中:
  • 在 Xcode 中,选择 File > New > Target。
  • 选择 Notification Content Extension。
  • 为其分配一个名称并完成设置。
通知内容扩展 - iOS 富推送通知

如果出现“激活方案”消息提示,请选择“取消”。

激活通知内容方案
  1. 按如下方式调整内容扩展中的属性和方法:
import UIKit
import UserNotifications
import UserNotificationsUI
import AVKit
class NotificationViewController: UIViewController, UNNotificationContentExtension {
var playerController: AVPlayerViewController!
@IBOutlet weak var playerBackgroundView: UIView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any required interface initialization here.
}
func didReceive(_ notification: UNNotification) {
let attachment = notification.request.content.attachments.first
playerController = AVPlayerViewController()
// Set height programmatically
// preferredContentSize.height = 250
if let url = attachment?.url {
setupVideoPlayer(url: url)
} else {
print("No valid URL...")
}
}
private func setupVideoPlayer(url: URL) {
guard let playerController = self.playerController else { return }
let player = AVPlayer(url: url)
playerController.player = player
playerController.view.frame = self.playerBackgroundView.bounds
playerBackgroundView.addSubview(playerController.view)
addChild(playerController)
playerController.didMove(toParent: self)
player.play()
}
  1. 将 UIView 合并到 MainInterface.storyboard 中:
MainInterface.storyboard 中的 UIView
  1. 将 playerBackgroundView IBOutlet 与您刚刚添加的 UIView 链接:
将 playerBackgroundView IBOutlet 与 UIView 链接
  1. 使用以下条目更新 info.plist 文件:
UNNotificationExtensionUserInteractionEnabled = true
UNNotificationExtensionUserInteractionEnabled = true

为了将视频附加到您的通知中,请在控制面板的媒体附件字段中输入视频的 URL:

Pushwoosh 控制面板中媒体附件字段中的视频 URL

通过 API /createMessage 请求发送通知时,请在 “ios_attachment” 参数中包含 URL,并确保 “mutable-content” 标志设置为 `1`。

自定义推送声音

Anchor link to

要在收到推送通知时播放自定义声音,首先请将音频文件放入您项目的根文件夹中。

然后,在推送参数中指定声音文件的名称——填写您消息的iOS 特定设置中的“声音”字段,或将文件名指定为 createMessage API 请求 的 “ios_sound” 参数的值。

用于自定义 iOS 声音的音频文件必须是以下格式之一:.aif.caf.wav请确保在文件名中指定格式;否则,Pushwoosh iOS SDK 将会忽略它。

iOS 临时推送

Anchor link to

工作原理

Anchor link to

临时推送通知会静默地出现在用户的通知中心,但不会出现在锁屏上。这种类型的推送不需要用户明确允许:只要用户安装并启动您的应用,您就可以开始发送它们。

然而,用户仍然可以订阅您的显式推送通知:当打开临时推送时,他们有两个选项来选择他们的体验——将推送保留在通知中心,不带警报和声音,或者允许您显式地发送推送,使其出现在锁屏上。

临时推送旨在让用户在是否愿意接收来自您应用的通知方面做出明智的决定。由于 APN 原生订阅请求只向用户显示一次,之后要订阅,他们必须进入手机的系统设置,一些用户可能因为不了解您的推送能带来什么价值而不会订阅。临时推送给予用户这种理解:他们可以看到您在推送通知中传递的内容,并决定是否需要显式地被通知这些内容。

如何实施

Anchor link to

1. 按照指南集成 Pushwoosh iOS SDK。

2. 在调用 registerForPushNotifications() 方法之前,将以下字符串添加到您项目的 AppDelegate 中:

if #available(iOS 12.0, *) {
Pushwoosh.sharedInstance().additionalAuthorizationOptions = UNAuthorizationOptions.provisional
}

就是这样!一旦用户安装了应用,他们将直接在通知中心收到消息。

与我们分享您的反馈

Anchor link to

您的反馈有助于我们创造更好的体验,因此如果您在 SDK 集成过程中遇到任何问题,我们非常希望听到您的声音。如果您遇到任何困难,请随时通过此表单与我们分享您的想法。