AppMetrica Push SDK 允许您向应用程序用户发送定向推送通知。这些通知被称为推送活动。您可以使用推送活动来吸引您的受众并帮助减少用户流失。
dependencies: [
.package(url: "https://github.com/appmetrica/push-sdk-ios", from: "3.0.0")
],
.target(
name: "YourTargetName",
dependencies: [
.product(name: "AppMetricaPush", package: "push-sdk-ios"),
// Optionally include 'AppMetricaPushLazy' for lazy push feature
]
)
pod init
。target 'YourAppName' do
# For all analytics features, add this umbrella module:
pod 'AppMetricaPush', '~> 3.0.0'
# Optionally add Lazy Push pod
pod 'AppMetricaPushLazy', '~> 3.0.0'
end
pod install
安装依赖项。.xcworkspace
文件在 Xcode 中打开您的项目。AppMetricaPush
:基本 SDK 使用所需。AppMetricaPushLazy
:启用延迟推送支持以下是如何将 AppMetrica Push SDK 添加到您的项目(适用于 SwiftUI 和 UIKit)
将 AppMetrica 集成到项目中
import AppMetricaPush
.
如果您正在使用通知服务扩展,请使用 共享 AppGroup 设置 AppMetrica 并在扩展中初始化 AppMetrica。
通过在 application(_:didFinishLaunchingWithOptions:)
中设置 UNUserNotificationCenter.current().delegate = AppMetricaPush.userNotificationCenterDelegate
来配置 AppMetricaPush。
在 application(_:didRegisterForRemoteNotificationsWithDeviceToken:)
中注册设备令牌
调用 UIApplication.registerForRemoteNotifications()
。 请参阅文档以启用可见通知。
(可选)如果您也使用 UISceneDelegate
,请在 scene(_:willConnectTo:options:)
中添加 AppMetricaPush.handleSceneWillConnectToSession(with:)
将此代码放入您的 AppDelegate.swift
文件中
import UIKit
import UserNotifications
import AppMetrica
import AppMetricaPush
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
if let configuration = AppMetricaConfiguration(apiKey: "Your_API_Key") {
AppMetrica.activate(with: configuration)
}
UNUserNotificationCenter.current().delegate = AppMetricaPush.userNotificationCenterDelegate
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
AppMetricaPush.setDeviceToken(deviceToken)
}
}
如果您使用场景,请将此代码放入您的 SceneDelegate.swift
文件中
import UIKit
import AppMetricaPush
class SceneDelegate: UIResponder, UIWindowSceneDelegate {
func scene(_ scene: UIScene, willConnectTo
session: UISceneSession, options
connectionOptions: UIScene.ConnectionOptions) {
AppMetricaPush.handleSceneWillConnectToSession(with: connectionOptions)
}
}
为了将 AppMetrica Push SDK 集成到 SwiftUI 应用程序中,您可以使用 AppDelegate 适配器来处理与推送通知相关的生命周期事件。 创建一个新的 AppDelegate.swift 并以类似于 UIKit 方法的方式进行设置
然后在您的 App
struct 中
@main
struct YourAppNameApp: App {
// Use the `@UIApplicationDelegateAdaptor` property wrapper to work with AppDelegate and set up AppMetrica Push SDK
@UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
如果您正在使用通知服务扩展,请将此代码放入您的 NotificationService.swift
文件中
class NotificationService: UNNotificationServiceExtension {
var contentHandler: ((UNNotificationContent) -> Void)?
var bestAttemptContent: UNMutableNotificationContent?
override func didReceive(_ request: UNNotificationRequest,
withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
// system does not always spawn new process to handle few notification, but AppMetrica ignore second initialization
let configuration = AppMetricaConfiguration(apiKey: "API-KEY")!
AppMetrica.activate(with: configuration)
self.contentHandler = contentHandler
bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)
// custom logic
AppMetrica.sendEventsBuffer()
}
您可以在我们的完整文档中找到全面的集成详细信息以及安装、配置、测试等说明。
AppMetrica Push SDK 在 MIT 许可证下发布。 许可协议可在 LICENSE 中找到。