AppMetrica Push SDK

CocoaPods Compatible SPM Index Swift Versions SPM Index Platforms

AppMetrica Push SDK 允许您向应用程序用户发送定向推送通知。这些通知被称为推送活动。您可以使用推送活动来吸引您的受众并帮助减少用户流失。

安装

Swift Package Manager

通过 Xcode

  1. 前往 File > Add Package Dependency
  2. 输入 AppMetrica Push SDK 的 GitHub 链接:https://github.com/appmetrica/push-sdk-ios
  3. Add to Target 中,对于您不需要的模块,选择 None

通过 Package.swift Manifest

  1. 将 SDK 添加到您的项目的依赖项中
dependencies: [
    .package(url: "https://github.com/appmetrica/push-sdk-ios", from: "3.0.0")
],
  1. 在您目标的依赖项中列出模块
.target(
    name: "YourTargetName",
    dependencies: [
        .product(name: "AppMetricaPush", package: "push-sdk-ios"),
        // Optionally include 'AppMetricaPushLazy' for lazy push feature
    ]
)

CocoaPods

  1. 如果您还没有设置 CocoaPods,请在您的项目目录中运行 pod init
  2. 在您的 Podfile 中,添加 AppMetrica Push 依赖项
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
  1. 使用 pod install 安装依赖项。
  2. 使用 .xcworkspace 文件在 Xcode 中打开您的项目。

可选

模块概览

集成快速入门

以下是如何将 AppMetrica Push SDK 添加到您的项目(适用于 SwiftUI 和 UIKit)

  1. AppMetrica 集成到项目中

  2. import AppMetricaPush.

  3. 如果您正在使用通知服务扩展,请使用 共享 AppGroup 设置 AppMetrica 并在扩展中初始化 AppMetrica。

  4. 通过在 application(_:didFinishLaunchingWithOptions:) 中设置 UNUserNotificationCenter.current().delegate = AppMetricaPush.userNotificationCenterDelegate 来配置 AppMetricaPush。

  5. application(_:didRegisterForRemoteNotificationsWithDeviceToken:) 中注册设备令牌

  6. 调用 UIApplication.registerForRemoteNotifications()。 请参阅文档以启用可见通知。

  7. (可选)如果您也使用 UISceneDelegate,请在 scene(_:willConnectTo:options:) 中添加 AppMetricaPush.handleSceneWillConnectToSession(with:)

对于 UIKit

将此代码放入您的 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)
	}
}

对于 SwiftUI

为了将 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 中找到。