appmetrica/push-sdk-ios
AppMetrica Push SDK for iOS
Installation
Swift Package Manager
Through Xcode:
- Go to File > Add Package Dependency.
- Put the GitHub link of the AppMetrica Push SDK: https://github.com/appmetrica/push-sdk-ios.
- In Add to Target, select None for modules you don't want.
Via Package.swift Manifest:
- Add the SDK to your project's dependencies:
dependencies: [
.package(url: "https://github.com/appmetrica/push-sdk-ios", from: "3.0.0")
],- List the modules in your target's dependencies:
.target(
name: "YourTargetName",
dependencies: [
.product(name: "AppMetricaPush", package: "push-sdk-ios"),
// Optionally include 'AppMetricaPushLazy' for lazy push feature
]
)CocoaPods
- If you haven't set up CocoaPods, run
pod initin your project directory. - In your Podfile, add AppMetrica Push dependencies:
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- Install the dependencies using
pod install. - Open your project in Xcode with the
.xcworkspacefile.
Optional
Modules Overview
AppMetricaPush: Required for basic SDK use.AppMetricaPushLazy: Enables lazy push support
Integration Quickstart
Here's how to add AppMetrica Push SDK to your project (works for both SwiftUI and UIKit):
- Add AppMetrica into project
import AppMetricaPush.
- If you are using notification service extension, set up AppMetrica with shared AppGroup and initialize AppMetrica in the extension.
- Configure AppMetricaPush by setting
UNUserNotificationCenter.current().delegate = AppMetricaPush.userNotificationCenterDelegateinapplication(_:didFinishLaunchingWithOptions:).
- Register device token in
application(_:didRegisterForRemoteNotificationsWithDeviceToken:)
- Call
UIApplication.registerForRemoteNotifications(). See documentation to enable visible notifications.
- (Optional) If you also use
UISceneDelegateaddAppMetricaPush.handleSceneWillConnectToSession(with:)inscene(_:willConnectTo:options:)
For UIKit:
Put this in your 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)
}
}If you are using scenes, put this in your 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)
}
}For SwiftUI:
For integrating AppMetrica Push SDK into a SwiftUI application, you can use an AppDelegate adapter to handle lifecycle events related to push notifications. Create a new AppDelegate.swift and set it up similarly to the UIKit approach:
Then in your 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()
}
}
}Notification Service Extension
If you are using notification service extension, put this in your 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()
}Documentation
You can find comprehensive integration details and instructions for installation, configuration, testing, and more in our full documentation.
License
AppMetrica Push SDK is released under the MIT License. License agreement is available at LICENSE.
Package Metadata
Repository: appmetrica/push-sdk-ios
Homepage: https://appmetrica.io
Stars: 4
Forks: 0
Open issues: 1
Default branch: main
Primary language: objective-c
License: MIT
Topics: appmetrica, ios, library, push
README: README.MD