Connecting the AppMetrica Push SDK

Step 1. Enable the library

The library can work with the following dependency managers:

The library supports static and dynamic frameworks for CocoaPods. To enable the library, add a dependency to the project's Podfile:

  • Static framework

    pod 'YandexMobileMetricaPush', '0.8.0'
  • Dynamic framework

    pod 'YandexMobileMetricaPush/Dynamic', '0.8.0'
If you don't use these dependency managers

To enable the library, follow these steps:

  1. Add YandexMobileMetricaPush.framework to the project.
Note. The AppMetrica SDK and AppMetrica Push SDK libraries must both be enabled in one of these ways.

Step 2. Register your app in the Apple Push Notification Service (APNs)

Registration prepares the app to work with push notifications. To send notifications to devices with iOS version 7 and higher, make the following changes to the application code:

if ([application respondsToSelector:@selector(registerForRemoteNotifications)]) {
    if (NSClassFromString(@"UNUserNotificationCenter") != Nil) {
        // iOS 10.0 and above
        UNAuthorizationOptions options =
            UNAuthorizationOptionAlert |
            UNAuthorizationOptionBadge |
        UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
        [center requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError *error) {
            // Enable or disable features based on authorization.
    else {
        // iOS 8 and iOS 9
        UIUserNotificationType userNotificationTypes =
            UIUserNotificationTypeAlert |
            UIUserNotificationTypeBadge |
        UIUserNotificationSettings *settings =
            [UIUserNotificationSettings settingsForTypes:userNotificationTypes categories:nil];
        [application registerUserNotificationSettings:settings];
    [application registerForRemoteNotifications];
This data is usually passed in the following method:
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

For more details about the methods used, see the documentation at

Step 3. Register a device token for your app

To send push notifications using AppMetrica, your app's device token is required. To register it, add the following code to your implementation of UIApplicationDelegate:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    // If the library AppMetrica the SDK was not initialized before this step,
    // the method call will result in emergency stop applications.
    [YMPYandexMetricaPush setDeviceTokenFromData:deviceToken];

To register the device token and send the APN environments, add the following code:

- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken
    // If the library AppMetrica the SDK was not initialized before this step,
    // the method call will result in emergency stop applications.
    #ifdef DEBUG
        YMPYandexMetricaPushEnvironment pushEnvironment = YMPYandexMetricaPushEnvironmentDevelopment;
        YMPYandexMetricaPushEnvironment pushEnvironment = YMPYandexMetricaPushEnvironmentProduction;
    [YMPYandexMetricaPush setDeviceTokenFromData:deviceToken pushEnvironment:pushEnvironment];
Attention. AppMetrica allows you to send push notifications to Sandbox APNs. However, push notification processing may not work correctly if versions of the application with different environments were run on the device(development and production). To avoid this issue, you can use a separate test API key for development environment.

Step 4. Configure handling the opening of push notifications.

Configure handling the opening of a push notification. To do this, add the following code to the corresponding UIApplicationDelegate methods:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    [YMPYandexMetricaPush handleApplicationDidFinishLaunchingWithOptions:launchOptions];
    return YES;
- (void)application:(UIApplication *)application
    didReceiveRemoteNotification:(NSDictionary *)userInfo
    [YMPYandexMetricaPush handleRemoteNotification:userInfo];
- (void)application:(UIApplication *)application
    didReceiveRemoteNotification:(NSDictionary *)userInfo
    fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
    [YMPYandexMetricaPush handleRemoteNotification:userInfo];

To integrate the SDK with iOS 10 and higher, add the following code:

#import <UserNotifications/UserNotifications.h>
//  In the  "- (BOOL)application:(UIApplication *)application 
//         didFinishLaunchingWithOptions:(NSDictionary *)launchOptions" method:
if ([UNUserNotificationCenter class] != Nil) {
    [UNUserNotificationCenter currentNotificationCenter].delegate =
        [YMPYandexMetricaPush userNotificationCenterDelegate];

If you already set another delegate to [UNUserNotificationCenter currentNotificationCenter].delegate, just set the delegate as a middleware before your own delegate:

[YMPYandexMetricaPush userNotificationCenterDelegate].nextDelegate = yourDelegate;

Step 5. (Optional) Enable push tokens update

The APNS service can withdraw the push token of the device, for example, if the user did not launch the application for a long time. AppMetrica stores push tokens on the server and can not send a push notification to a device with an obsolete token.

To automatically collect current push token go to the application settings in the AppMetrica interface and enable the Update tokens with a Silent Push notification option in the Push Notifications tab.

Sending additional information

You can send additional information with the push notification if necessary. This data is specified in the AppMetrica web interface when configuring the push campaign. To get this information, use the following method:

NSString *userData = [YMPYandexMetricaPush userDataForNotification:userInfo];

where userInfo contains information about the push notification.

Defining the recipient of a notification

AppMetrica allows you to detect "own" push notifications, if several Push SDKs were built into the application.

To detect, if the AppMetrica is the recipient of a notification, use the following method:

BOOL isRelatedToAppMetricaSDK = [YMPYandexMetricaPush isNotificationRelatedToSDK:userInfo];