Push4Site
Push4Site 是官方的 iOS 框架,它提供对 Push4Site.com API 的访问。
安装
Push4Site 通过 CocoaPods 提供。要安装它,只需将以下行添加到 Podfile:
pod 'Push4Site'
初始化
要初始化 Push4Site SDK,可以参考以下示例中的步骤。
// AppDelegate.swift
// ...
import Push4Site // First, import Push4Site module
// ...
func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
// Ininitialize SDK with token, which can be obtained in Push4Site.com control panel.
Push4Site.shared.configure(with: "dacd7f46202c11eba814309c23d3543b",
launchOptions: launchOptions)
return true
}
// ...
然后通过在需要请求用户订阅权限的地方调用适当的方法来订阅远程通知。
Push4Site.shared.subscribeForNotifications()
这就是使用 Push4Site SDK 所需要的一切。
自定义通知外观
如果您需要自定义远程通知(例如展示图片),那么也需要安装 Push4Site Notification Helper。
pod 'Push4Site-UN'
然后在项目中创建一个 Notification Service Extension
类型的目标。在创建目标之后,打开其 NotificationService.swift
文件的起点,并将通知请求数组传递给 Push4Site SDK。
override func didReceive(
_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void
) {
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
NotificationServiceHelper.shared.process(request: request,
contentHandler: contentHandler)
}
现在您的应用就可以在远程通知中显示图片了。
发送自定义事件
要发送自定义事件,只需调用相应的SDK方法
let eventId = "my_custom_event"
Push4Site.shared.logEvent(with: eventId)
示例
一个示例应用程序,展示了如何与Push4Site SDK一起工作,可以在SDK的GitHub仓库中找到。
许可证
Push4Site遵循MIT许可证,更多信息请参阅LICENSE文件。