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 通知助手
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)
示例
可以在SDK的GitHub仓库中找到一个示例应用程序,展示了如何使用Push4Site SDK。
许可证
Push4Site遵循MIT许可证。有关更多信息,请参阅LICENSE文件。