---
metadata:
  - name: generator
    content: Diplodoc Platform v5.50.6
alternate:
  - https://appmetrica.yandex.by/docs/en/sdk/ios/push/quick-start.md
  - https://appmetrica.yandex.by/docs/ru/sdk/ios/push/quick-start.md
---
> **Documentation Index:** Fetch the complete configuration index at https://appmetrica.yandex.by/docs/ru/llms.txt

# Подключение и инициализация

Перед началом работы с AppMetrica Push SDK версии 3.4.0 необходимо [подключить и инициализировать AppMetrica SDK](https://appmetrica.yandex.by/docs/ru/sdk/ios/analytics/quick-start.md) не ниже версии 5.9.0.

## Шаг 1. Подключите библиотеку {#installation}

Библиотека может работать со следующими системами управления зависимостями:

{% list tabs %}

- CocoaPods

  Чтобы подключить библиотеку Push SDK, добавьте в Podfile проекта зависимости:

  ```ruby translate=no
  pod 'AppMetricaPush', '~> 3.4.0' # Основной модуль для работы с Push SDK, обязателен для подключения
  pod 'AppMetricaPushLazy', '~> 3.4.0' # Дополнительный модуль для Lazy пушей
  ```

- SPM в Xcode

  **Интеграция через интерфейс Xcode**

  Чтобы подключить библиотеку, выполните следующее:

  1. В окне Xcode навигатора проектов (**Project Navigator**) выберите свой проект (если у вас используется **Workspace**). Затем в верхнем меню нажмите **File** и выберите **Add Package Dependencies...**.

     ![проект](../../../../_images/xcode_add_package_dependency.png)

  2. Укажите URL репозитория `https://github.com/appmetrica/push-sdk-ios`.

     ![URL репозитория](../../../../_images/spm-url-push.png)

  3. Оставьте только нужные модули (чтобы выключить модуль в `Add to Target` выберите значение `None`):

     ![нужные модули](../../../../_images/spm-select-packages-push.png)

     * `AppMetricaPush` — обязательный основной модуль Push SDK. Должен быть подключен для работы.
     * `AppMetricaPushLazy` — дополнительный модуль для Lazy пушей.

- Package.swift

  **Интеграция через Package.swift манифест**

  Для начала добавьте следующую зависимость в массив `dependencies:` вашего пакета:

   ```swift translate=no
   dependencies: [
      .package(
         url: "https://github.com/appmetrica/push-sdk-ios",
         from: "3.4.0"
      )
   ],
   ```

   Модули AppMetrica Push SDK, которые вы можете подключить в зависимости от потребностей вашего проекта:

   * `AppMetricaPush` — обязательный основной модуль Push SDK. Должен быть подключен для работы.
   * `AppMetricaPushLazy` — дополнительный модуль для Lazy пушей.

   Пример подключения модулей в массиве `dependencies:` таргета:

   ```swift translate=no
   .target(
      name: "MyTargetName",
      dependencies: [
         .product(name: "AppMetricaPush", package: "push-sdk-ios"),
         // .product(name: "AppMetricaPushLazy", package: "push-sdk-ios"), // Этот модуль отлючен
      ]
   ),
   ```

{% endlist %}


### Шаг 2. Настройте AppMetricaPush {#setup}

{% list tabs group=instructions %}

- Objective-C

  Добавьте следующий код в соответствующий метод реализации `UIApplicationDelegate`:

  ```objectivec translate=no
  - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  {
      AMAAppMetricaConfiguration *configuration = [[AMAAppMetricaConfiguration alloc] initWithAPIKey:@"YOUR-API-KEY"];
      [AMAAppMetrica activateWithConfiguration:configuration];

      ...
      [AMPAppMetricaPush setExtensionAppGroup:appGroup]; // optional, need to send stored events from AppMetricaPush-2.2 and earlier
      ...
      [AMPAppMetricaPush handleApplicationDidFinishLaunchingWithOptions:launchOptions];
      ...
  }
  ```

- Swift

  Добавьте следующий код в соответствующий метод реализации `AppDelegate`:

  ```objectivec translate=no
  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey :Any]? = nil) -> Bool
  {
      // Activating AppMetrica.
      let configuration = AppMetricaConfiguration(apiKey: "YOUR-API-KEY")!
      AppMetrica.activate(with: configuration)
      ...
      AppMetricaPush.setExtensionAppGroup(appGroup) // optional, need to send stored events from AppMetricaPush-2.2 and earlier
      ...
      AppMetricaPush.handleApplicationDidFinishLaunching(options: launchOptions)
      ...
  }
  ```

{% endlist %}

`appGroup` — название общей группы App Groups.


### Cбор статистики отклонений {#dismiss}

Чтобы собирать статистику отклонений push-уведомлений, задайте для категории уведомлений `UNNotificationCategory` следующую опцию:

{% list tabs group=instructions %}

- Objective-C

  ```objectivec translate=no
  options:UNNotificationCategoryOptionCustomDismissAction
  ```

- Swift

  ```swift translate=no
  options: UNNotificationCategory.customDismissAction
  ```

{% endlist %}


## Шаг 3. (_Опционально_) Настройка AppMetrica для работы в Notification Service Extension {#notification}

Добавьте Notification Service Extension через XCode.

1. В Xcode выберите **File** → **New** → **Target**.
2. В разделе расширений **iOS** выберите из списка **Notification Service Extension** и нажмите **Next**.
3. Введите название расширения в поле **Product Name** и нажмите **Finish**.

Настройте AppMetrica и расширение, см. [Поддержка App Extensions](https://appmetrica.yandex.by/docs/ru/sdk/ios/analytics/ios-appgroup.md).

1. Создайте в [developer.apple.com](https://developer.apple.com/account/resources/identifiers/list/bundleId) Bundle ID для Notification Service Extension.

2. Создайте в [developer.apple.com](https://developer.apple.com/account/resources/identifiers/list/applicationGroup) AppGroup.

3. Добавьте AppGroup в [provisioning profile](https://developer.apple.com/account/resources/profiles/list) приложения и Notification Service Extension. Убедитесь, что AppGroup доступна только приложению и его экстеншенам. Если AppGroup доступна нескольким приложениям, то AppMetrica будет неправильно считать установки.

4. Откройте Xcode и добавьте AppGroup и Keychain Access Group в таргет приложения.

   ![в таргет приложения](../../../../_images/xcode-push-app.png){style="border: solid 1px #cccccc; max-width: 800px;"}

5. Откройте Xcode и добавьте AppGroup и Keychain Access Group в таргет NSE.

   ![в таргет NSE](../../../../_images/xcode-push-nse.png){style="border: solid 1px #cccccc; max-width: 800px;"}

6. Добавьте AppGroup в `Info.plist`.

   ![в Info.plist](../../../../_images/xcode-push-infoplist.png){style="border: solid 1px #cccccc; max-width: 800px;"}

7. Проверьте entitlements и убедитесь, что там добавлены AppGroup и Keychain Access Group.

   ![проверьте entitlements](../../../../_images/xcode-push-entitlements.png){style="border: solid 1px #cccccc; max-width: 800px;"}

8. Реализуйте`NotificationService`.

`AppMetricaPush` в Notification Service Extension берет конфигурацию из приложения и автоматически активирует AppMetrica.
Пример реализации приведен ниже:

{% list tabs group=instructions %}

- Swift

  ```swift translate=no
  class NotificationService: UNNotificationServiceExtension {

      var contentHandler: ((UNNotificationContent) -> Void)?
      var bestAttemptContent: UNMutableNotificationContent?

      override func didReceive(_ request: UNNotificationRequest,
                              withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
         AppMetricaPush.setExtensionAppGroup(appGroup) // optional, need to send stored events from AppMetricaPush-2.2 and earlier

         AMPAppMetricaPush.handleDidReceive(request)

         self.contentHandler = contentHandler
         bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

         // custom logic

         AppMetrica.sendEventsBuffer()
      }

      override func serviceExtensionTimeWillExpire() {
         bestAttemptContent?()
      }
  } 
  ```

- Objective-C

  ```objectivec translate=no
  @implementation NotificationService

  + (void)initialize {
      if ([self class] == [NotificationService class]) {
          [AMPAppMetricaPush setExtensionAppGroup:appGroup]; // push saved events
      }
  }

  - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request
                     withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {

      [AMPAppMetricaPush handleDidReceiveNotificationRequest:request];

      self.contentHandler = contentHandler;
      self.bestAttemptContent = [request.content mutableCopy];

      // custom logic

      [AMAAppMetrica sendEventsBuffer]; // force send all events
  }

  - (void)serviceExtensionTimeWillExpire {
    if (self.bestAttemptContent != nil) {
      self.bestAttemptContent();
    }

  }

  @end
  ```

{% endlist %}


Также есть возможность активировать AppMetrica c другой конфигурацией. 

{% list tabs group=instructions %}

- Swift

  ```swift translate=no
  class NotificationService: UNNotificationServiceExtension {

      var contentHandler: ((UNNotificationContent) -> Void)?
      var bestAttemptContent: UNMutableNotificationContent?

      func activateIfNeeded() {
         // system does not always spawn new process to handle few notification, but AppMetrica ignores second initialization
        let configuration = AppMetricaConfiguration(apiKey: "API-KEY")! // your custom configuration
        AppMetrica.activate(with: configuration)

        AppMetricaPush.setExtensionAppGroup(appGroup) // optional, need to send stored events from AppMetricaPush-2.2 and earlier
      }

      override func didReceive(_ request: UNNotificationRequest,
                              withContentHandler contentHandler: @escaping (UNNotificationContent) -> Void) {
         activateIfNeeded()

        AMPAppMetricaPush.handleDidReceive(request)

        self.contentHandler = contentHandler
        bestAttemptContent = (request.content.mutableCopy() as? UNMutableNotificationContent)

         // custom logic

        AppMetrica.sendEventsBuffer()
      }

      override func serviceExtensionTimeWillExpire() {
         bestAttemptContent?()
      }
  }
  ```

- Objective-C

  ```objectivec translate=no
  @implementation NotificationService

  + (void)initialize {
      if ([self class] == [NotificationService class]) {
          AMAAppMetricaConfiguration *configuration =
              [[AMAAppMetricaConfiguration alloc] initWithAPIKey:@"API-KEY"]; // your custom configuration
          [AMAAppMetrica activateWithConfiguration:configuration];

          [AMPAppMetricaPush setExtensionAppGroup:appGroup]; // push saved events
      }
  }

  - (void)didReceiveNotificationRequest:(UNNotificationRequest *)request
                     withContentHandler:(void (^)(UNNotificationContent * _Nonnull))contentHandler {

      [AMPAppMetricaPush handleDidReceiveNotificationRequest:request];

      self.contentHandler = contentHandler;
      self.bestAttemptContent = [request.content mutableCopy];

      // custom logic

      [AMAAppMetrica sendEventsBuffer]; // force send all events
  }

  - (void)serviceExtensionTimeWillExpire {
    if (self.bestAttemptContent != nil) {
      self.bestAttemptContent();
    }
  }

  @end
  ```

{% endlist %}

## Шаг 4. Зарегистрируйте приложение в Apple Push Notification Service (APNs) {#apns}

Регистрация необходима для подготовки приложения к работе с push-уведомлениями. Для отправки уведомлений на устройства с iOS добавьте следующие изменения в код приложения:

{% list tabs group=instructions %}

- Swift

  ```swift translate=no
  let center = UNUserNotificationCenter.current()
  center.requestAuthorization(options:\[.badge, .alert, .sound\]) { (granted, error) in
    // Enable or disable features based on authorization.
  }
  application.registerForRemoteNotifications()
  ```

  Как правило, эти данные передаются в следующем методе:

  ```swift translate=no
  func application(_ application: UIApplication, didFinishLaunchingWithOptions
                   launchOptions: [UIApplicationLaunchOptionsKey :Any]? = nil) -> Bool
  ```

  Подробно об используемых методах в документации на `developer.apple.com`:

  * [UNUserNotificationCenter.current()](https://developer.apple.com/documentation/usernotifications/unusernotificationcenter/1649510-current)
  * [UNUserNotificationCenter.requestAuthorization(options:completionHandler:)](https://developer.apple.com/documentation/usernotifications/unusernotificationcenter/1649527-requestauthorization)
  * [UIApplication.registerForRemoteNotifications()](https://developer.apple.com/documentation/uikit/uiapplication/1623078-registerforremotenotifications)

- Objective-C

  ```objectivec translate=no
  UNAuthorizationOptions options =
    UNAuthorizationOptionAlert |
      UNAuthorizationOptionBadge |
      UNAuthorizationOptionSound;
  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  [center requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError *error) {
    // Enable or disable features based on authorization.
  }];
  [application registerForRemoteNotifications];
  ```

  Как правило, эти данные передаются в следующем методе:

  ```objectivec translate=no
   - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
  ```

  Подробно об используемых методах в документации на `developer.apple.com`:

  * [[UNUserNotificationCenter currentNotificationCenter]](https://developer.apple.com/documentation/usernotifications/unusernotificationcenter/1649510-currentnotificationcenter?language=objc)
  * [[UNUserNotificationCenter requestAuthorizationWithOptions:completionHandler:]](https://developer.apple.com/documentation/usernotifications/unusernotificationcenter/1649527-requestauthorizationwithoptions?language=objc)
  * [[UIApplication registerForRemoteNotifications]](https://developer.apple.com/documentation/uikit/uiapplication/1623078-registerforremotenotifications?language=objc)

{% endlist %}

## Шаг 5. Зарегистрируйте device token вашего приложения {#device-token}

Для отправки push-уведомления с помощью AppMetrica необходим device token вашего приложения. Чтобы зарегистрировать его:

{% list tabs group=instructions %}

- Swift

  Добавьте следующий код в `AppDelegate`:

  ```swift translate=no
  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
  {
      // If the AppMetrica SDK library was not initialized before this step,
      // calling the method causes the app to crash.
      AppMetricaPush.setDeviceTokenFrom(deviceToken)
  }
  ```

- Objective-C

  Добавьте следующий код в вашу реализацию `UIApplicationDelegate`:

  ```objectivec translate=no
  - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
  {
      // If the AppMetrica SDK library was not initialized before this step,
      // calling the method causes the app to crash.
      [AMPAppMetricaPush setDeviceTokenFromData:deviceToken];
  }
  ```

{% endlist %}

Чтобы зарегистрировать device token и отправить окружение APNs, используйте следующий код:

{% list tabs group=instructions %}

- Swift

  ```swift translate=no
  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
  {
      // If the AppMetrica SDK library was not initialized before this step,
      // calling the method causes the app to crash.
      #if DEBUG
          let pushEnvironment = AppMetricaPushEnvironment.development
      #else
          let pushEnvironment = AppMetricaPushEnvironment.production
      #endif
      AppMetricaPush.setDeviceTokenFrom(deviceToken, pushEnvironment: pushEnvironment)
  }
  ```

- Objective-C

  ```objectivec translate=no
  - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
  {
      // If the AppMetrica SDK library was not initialized before this step,
      // calling the method causes the app to crash.
      #ifdef DEBUG
          AMPAppMetricaPushEnvironment pushEnvironment = AMPAppMetricaPushEnvironmentDevelopment;
      #else
          AMPAppMetricaPushEnvironment pushEnvironment = AMPAppMetricaPushEnvironmentProduction;
      #endif
      [AMPAppMetricaPush setDeviceTokenFromData:deviceToken pushEnvironment:pushEnvironment];
  }
  ```

{% endlist %}

{% note alert %}

AppMetrica позволяет отправлять push-уведомления в Sandbox APNs. Но обработка push-уведомлений может работать некорректно, если на устройстве запускались версии приложения с разным окружением (_development_ и _production_). Чтобы избежать этого, можно использовать отдельный тестовый API key для _development_ окружения.

{% endnote %}

## Шаг 6. Настройте обработку открытия push-уведомления {#opening}

Настройте обработку открытия push-уведомления:

1. Воспользуйтесь делегатом `UserNotificationCenterDelegate`/`AMPUserNotificationCenterDelegate`. Он обрабатывает получение push-уведомлений автоматически, при их открытии.

    Внесите следующие изменения в код:

    {% list tabs group=instructions %}

    - Swift

      ```swift translate=no
      import UserNotifications

      // In the "func application(_ application: UIApplication, didFinishLaunchingWithOptions
      //                          launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool" method:
      let delegate = AppMetricaPush.userNotificationCenterDelegate
      UNUserNotificationCenter.current().delegate = delegate
      ```

    - Objective-C

      ```objectivec translate=no
      #import <UserNotifications/UserNotifications.h>

      // In the   "- (BOOL)application:(UIApplication *)application
      // didFinishLaunchingWithOptions:(NSDictionary *)launchOptions" method:
      [UNUserNotificationCenter currentNotificationCenter].delegate =
              [AppMetricaPush userNotificationCenterDelegate];
      ```

    {% endlist %}

    Чтобы отслеживать открытие и другие действия с push-уведомлениями, создайте собственный делегат `UNUserNotificationCenterDelegate` и добавьте его в `nextDelegate`:

    {% list tabs group=instructions %}

    - Swift

      ```swift translate=no
      AppMetricaPush.userNotificationCenterDelegate.nextDelegate = yourDelegate
      ```

    - Objective-C

      ```objectivec translate=no
      [AMPAppMetricaPush userNotificationCenterDelegate].nextDelegate = yourDelegate;
      ```

    {% endlist %}

    После этого можно использовать соответствующие методы своего делегата.

2. Если вы дополнительно используете [UISceneDelegate](https://developer.apple.com/documentation/uikit/uiscenedelegate), добавьте следующий код в метод `scene(_:willConnectTo:options:)`:

   {% list tabs group=instructions %}

   - Swift

     ```swift translate=no
     func scene(_ scene: UIScene, willConnectTo
                session: UISceneSession, options
                connectionOptions: UIScene.ConnectionOptions) {
          AppMetricaPush.handleSceneWillConnectToSession(with: connectionOptions)
     }
     ```

   - Objective-C

     ```objectivec translate=no
     - (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session
           options:(UISceneConnectionOptions *)connectionOptions
     {
         [AMPAppMetricaPush handleSceneWillConnectToSessionWithOptions:connectionOptions];
     }
     ```

   {% endlist %}

## Шаг 7. (_Опционально_) Включите актуализацию push-токенов {#actualization}

Если push-токен устройства стал неактуальным (например, пользователь удалил приложение), сервис APNS может вернуть ошибку 410 «The device token is no longer active for the topic». AppMetrica может поддерживать список актуальных токенов с помощью отправки silent push-уведомлений. Это поможет вам получить представление о количестве устройств до начала пуш-кампании.

Для этого в веб-интерфейсе AppMetrica перейдите в настройки приложения и во вкладке **Push-уведомления** выберите опцию **Актуализировать токены с помощью Silent Push-уведомлений**.

Использование silent push повышает точность оценки аудитории пуш-компании и отчетов по пушам и может повлиять на успешность доставки push-уведомлений приложения.

## Шаг 8. (_Опционально_) Настройте загрузку прикрепленных файлов {#download-file}

{% note info %}

В веб-интерфейсе push-кампаний функциональность недоступна.

{% endnote %}

Вы можете настроить загрузку прикрепленных файлов в push-уведомлениях:

1. Настройте загрузку прикрепленных файлов с помощью метода [downloadAttachmentsForNotificationRequest](https://appmetrica.yandex.by/docs/ru/sdk/ios/push/objectivec/AMPAppMetricaPush.md#method_downloadAttachmentsForNotificationRequest) Push SDK. С примером интеграции можно ознакомиться в статье [Загрузка прикрепленных файлов](https://appmetrica.yandex.by/docs/ru/sdk/ios/push/ios-push-download-file.md).
2. Прикрепите вложения (параметр `attachments`) с помощью операции [Отправка push-сообщений](https://appmetrica.yandex.by/docs/ru/mobile-api/push/post-send-batch.md) Push API.

## Отправка дополнительной информации {#send-additional-info}

При необходимости вы можете передавать вместе с push-уведомлением дополнительную информацию. Эти данные указываются в веб-интерфейсе AppMetrica при [настройке push-кампании](https://appmetrica.yandex.by/docs/ru/push/marketing.md). Чтобы получать эту информацию, используйте метод:

{% list tabs group=instructions %}

- Swift

  ```swift translate=no
  let userData = AppMetricaPush.userData(forNotification: userInfo)
  ```

- Objective-C

  ```objectivec translate=no
  NSString *userData = [AMPAppMetricaPush userDataForNotification:userInfo];
  ```

{% endlist %}

где `userInfo` содержит данные о push-уведомлении.

## Определение получателя уведомления {#detect-internal-push}

AppMetrica позволяет определить свои push-уведомления, если в приложение были встроены несколько Push SDK.

Чтобы определить, является ли AppMetrica получателем сообщения, используйте следующий метод:

{% list tabs group=instructions %}

- Swift

  ```swift translate=no
  let isRelatedToAppMetricaSDK = AppMetricaPush.isNotificationRelated(toSDK: userInfo)
  ```

- Objective-C

  ```objectivec translate=no
  BOOL isRelatedToAppMetricaSDK = [AMPAppMetricaPush isNotificationRelatedToSDK:userInfo];
  ```

{% endlist %}

### См. также

- [Пример интеграции библиотеки](https://github.com/yandexmobile/metrica-push-sdk-ios)
- [Настройка приложения на базе iOS для отправки push-уведомлений](https://appmetrica.yandex.by/docs/ru/sdk/ios/push/ios-settings.md)

<!-- source: ru/_includes/feedback-button-3.md -->
Если вы не нашли ответ на свой вопрос, то вы можете задать его через форму обратной связи. Пожалуйста, опишите возникшую проблему как можно подробнее. Если возможно, приложите скриншот.

<a href="../../../troubleshooting/feedback-new">
  <span class="button">Написать в службу поддержки</span>
</a>

<a href="../../../troubleshooting/feedback-docs">
  <span class="button">Предложить улучшение для документации</span>
</a>
<!-- endsource: ru/_includes/feedback-button-3.md -->
