---
metadata:
  - name: generator
    content: Diplodoc Platform v5.64.0
alternate:
  - https://boost.yandex.com/doc/en/analytics/content/sdk/ios/push/objectivec/AMPAppMetricaPush.md
  - https://boost.yandex.com/doc/ru/analytics/content/sdk/ios/push/objectivec/AMPAppMetricaPush.md
  - https://boost.yandex.com/doc/zh/analytics/content/sdk/ios/push/objectivec/AMPAppMetricaPush.md
  - href: https://boost.yandex.com/doc/zh/analytics/content/sdk/ios/push/objectivec/AMPAppMetricaPush.md
    type: text/markdown
    title: Markdown version
  - href: https://boost.yandex.com/doc/zh/llms.txt?revision=r21772739
    rel: describedby
---
> **Documentation Index:** Fetch the complete configuration index at https://boost.yandex.com/doc/zh/llms.txt?revision=r21772739

# AMPAppMetricaPush 类

处理推送通知的主要类。

## Instance methods

#|
|| +[downloadAttachmentsForNotificationRequest:](#method_downloadAttachmentsForNotificationRequest) | 在推送通知中上传附件。 该方法适用于 iOS 10.0 及更高版本。 ||
|| +[handleApplicationDidFinishLaunchingWithOptions:](#method_handleApplicationDidFinishLaunchingWithOptions) | 处理来自方法 [application:didFinishLaunchingWithOptions:](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622921-application?language=objc) 的推送通知打开事件。 ||
||+[handleDidReceiveNotificationRequest:](#method_handleDidReceiveNotificationRequest) | 处理来自通知服务扩展的推送通知接收。 ||
|| [handleRemoteNotification:](#method_handleRemoteNotification) | 处理来自方法 [application:didReceiveRemoteNotification:fetchCompletionHandler:](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623013-application?language=objc) 的推送通知打开。 ||
|| +[handleSceneWillConnectToSession:](#method_handleSceneWillConnectToSession) | 处理从方法 [scene:willConnectToSession:options:](https://developer.apple.com/documentation/uikit/uiscenedelegate/3197914-scene?language=objc) 打开的推送通知。 ||
|| +[isNotificationRelatedToSDK:](#method_isNotificationRelatedToSDK) | Returns `是`如果推送通知与AppMetrica相关。 ||
|| +[setDeviceTokenFromData:](#method_setDeviceTokenFromData) | 为应用程序的生产环境注册设备令牌。 ||
|| +[setDeviceTokenFromData:pushEnvironment:](#method_setDeviceTokenFromData_pushEnvironment) | 将应用程序的设备令牌注册到指定环境中。 ||
|| +[setExtensionAppGroup:](#method_setExtensionAppGroup) | 为应用和通知服务扩展注册共享的 App Groups 组。 ||
|| +[userDataForNotification:](#method_userDataForNotification) | 返回在推送通知中传递的任意数据字符串。 ||
|#

## Properties {#property_summary}

#|
|| [userNotificationCenterDelegate](#property_userNotificationCenterDelegate) | 返回一个代理 [AMPUserNotificationCenterDelegate](https://boost.yandex.com/doc/zh/analytics/content/sdk/ios/push/objectivec/AMPUserNotificationCenterDelegate.md)，用于处理 iOS 10 及更高版本的前台推送通知。 ||
|| [userNotificationCenterHandler](#property_userNotificationCenterHandler) | 返回一个委托 [AMPUserNotificationCenterHandling](https://boost.yandex.com/doc/zh/analytics/content/sdk/ios/push/objectivec/AMPUserNotificationCenterHandling.md)，它允许你手动处理 iOS 10 及更高版本的前台推送通知。 ||
|#

## Method descriptions {#method_detail}

### +downloadAttachmentsForNotificationRequest: {#method_downloadAttachmentsForNotificationRequest}

```objectivec translate=no
+ (void)downloadAttachmentsForNotificationRequest:(UNNotificationRequest *)request
                                         callback:(AMPAttachmentsDownloadCallback)callback);
```

在推送通知中上传附件。 该方法适用于 iOS 10.0 及更高版本。

**参数：**

#|
|| `请求` | [UNNotificationRequest](https://developer.apple.com/documentation/usernotifications/unnotificationrequest) 的实例. ||
|| `callback` | 上传附件的回调块。 Format:

```objectivec translate=no
typedef void (^AMPAttachmentsDownloadCallback) (NSArray<UNNotificationAttachment *> * _Nullable attachments, NSError * _Nullable error)
```
包括一个附件数组`附件`和一个`错误`，如果在上传文件时发生错误。 ||
|#

### +handleApplicationDidFinishLaunchingWithOptions: {#method_handleApplicationDidFinishLaunchingWithOptions}

```objectivec translate=no
+ (void)handleApplicationDidFinishLaunchingWithOptions:(nullable NSDictionary *)launchOptions
```

处理来自方法 [application:didFinishLaunchingWithOptions:](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622921-application?language=objc) 的推送通知打开事件。 Method should be invoked after AppMetrica SDK初始化。

**参数：**

#|
|| `launchOptions` | 参数以“键-值”对的形式提供，包含关于应用程序启动的信息。 ||
|#

### +handleDidReceiveNotificationRequest: {#method_handleDidReceiveNotificationRequest}

```objectivec translate=no
+ (void)handleDidReceiveNotificationRequest:(UNNotificationRequest *)request
```

处理来自通知服务扩展的推送通知接收。

您应该在 [didReceiveNotificationRequest:withContentHandler:](https://developer.apple.com/documentation/usernotifications/unnotificationserviceextension/1648229-didreceivenotificationrequest) 的实现中调用该方法。

**参数：**

#|
|| `请求` | [UNNotificationRequest](https://developer.apple.com/documentation/usernotifications/unnotificationrequest?language=objc) 的实例. ||
|#

### +handleRemoteNotification: {#method_handleRemoteNotification}

```objectivec translate=no
+ (void)handleRemoteNotification:(NSDictionary *)userInfo
```

处理来自方法 [application:didReceiveRemoteNotification:fetchCompletionHandler](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1623013-application?language=objc) 的推送通知打开。 Method should be invoked after AppMetrica SDK初始化。

**参数：**

#|
|| `userInfo` | 推送通知的参数，以“键-值”对的形式由系统传输。 ||
|#

### +handleSceneWillConnectToSession: {#method_handleSceneWillConnectToSession}

```objectivec translate=no
+ (void)handleSceneWillConnectToSessionWithOptions:(UISceneConnectionOptions *)connectionOptions
```

处理来自方法 [scene:willConnectToSession:options:](https://developer.apple.com/documentation/uikit/uiscenedelegate/3197914-scene?language=objc) 的推送通知打开。 应在 AppMetrica SDK 初始化后调用此方法.

**参数：**

#|
|| `connectionOptions` | 系统传输的 [UISceneConnectionOptions](https://developer.apple.com/documentation/uikit/uisceneconnectionoptions?language=objc) 类对象，包含连接参数。 ||
|#

### +isNotificationRelatedToSDK: {#method_isNotificationRelatedToSDK}

```objectivec translate=no
+ (BOOL)isNotificationRelatedToSDK:(NSDictionary *)userInfo;
```

Returns `是`如果推送通知与AppMetrica相关。

**参数：**

#|
|| `userInfo` | 推送通知的参数，以“键-值”对的形式由系统传输。 ||
|#

**返回：**

- `是` — 如果推送通知与AppMetrica相关。
- `否` — 如果推送通知与AppMetrica无关。

### +setDeviceTokenFromData: {#method_setDeviceTokenFromData}

```objectivec translate=no
+ (void)setDeviceTokenFromData:(nullable NSData *)data
```

为应用的生产环境注册设备令牌。 Method should be invoked after AppMetrica SDK初始化。

**参数：**

#|
|| `数据` | 应用的设备令牌。

如果你传递 `nil` 值，前一个设备令牌将被撤销。 ||
|#

### +setDeviceTokenFromData:pushEnvironment: {#method_setDeviceTokenFromData_pushEnvironment}

```objectivec translate=no
+ (void)setDeviceTokenFromData:(nullable NSData *)data
               pushEnvironment:(AMPAppMetricaPushEnvironment)pushEnvironment
```

将应用程序的设备令牌注册到指定环境中。 Method should be invoked after AppMetrica SDK初始化。

{% note alert %}

AppMetrica 允许您向 Sandbox APNs 发送推送通知。 但是，如果设备上运行了应用的不同环境版本（_开发_ 和 _生产_），推送通知的处理可能会工作不正常。 为了避免这个问题，你可以为_开发_环境使用一个单独的测试 API 密钥。

{% endnote %}

**参数：**

#|
|| `数据` | 应用的设备令牌。

如果你传递 `nil` 值，前一个设备令牌将被撤销。 ||
|| `pushEnvironment` | APNs 应用环境。 ||
|#

### +setExtensionAppGroup: {#method_setExtensionAppGroup}

```objectivec translate=no
+ (void)setExtensionAppGroup:(NSString *)appGroup
```

为应用和通知服务扩展注册共享的 App Groups。

需要注册才能跟踪推送通知的送达。 要获取更多信息，请查看[配置推送通知统计收集](https://boost.yandex.com/doc/zh/analytics/content/sdk/ios/push/ios-settings.md)。

**参数：**

#|
|| `appGroup` | 共享App Groups组的名称。 ||
|#

### +userDataForNotification: {#method_userDataForNotification}

```objectivec translate=no
+ (nullable NSString *)userDataForNotification:(NSDictionary *)userInfo
```

返回在推送通知中传递的任意数据字符串：

- 在从 AppMetrica 界面发送时的 **附加数据** 字段中。
- 在`数据`字段中发送时使用[Push API](https://boost.yandex.com/doc/zh/analytics/content/mobile-api/push/about.md)。

**参数：**

#|
|| `userInfo` | 推送通知的参数，以“键值对”的形式由系统传输。 ||
|#

**返回：**

任意数据字符串。

## Property descriptions {#property_detail}

### +userNotificationCenterDelegate {#property_userNotificationCenterDelegate}

```objectivec translate=no
@property (readonly, class) id<AMPUserNotificationCenterDelegate> userNotificationCenterDelegate;
```

返回一个代理 [AMPUserNotificationCenterDelegate](https://boost.yandex.com/doc/zh/analytics/content/sdk/ios/push/objectivec/AMPUserNotificationCenterDelegate.md)，用于处理 iOS 10 及更高版本的前台推送通知。

要处理前台推送通知，请将此代码添加到 [application: didFinishLaunchingWithOptions:](https://developer.apple.com/documentation/uikit/uiapplicationdelegate/1622921-application?language=objc) 方法中：

```objectivec translate=no
[UNUserNotificationCenter currentNotificationCenter].delegate = [AMPAppMetricaPush userNotificationCenterDelegate];
```

要手动处理推送通知，请使用 [userNotificationCenterHandler](#property_userNotificationCenterHandler)。

### +userNotificationCenterHandler {#property_userNotificationCenterHandler}

```objectivec translate=no
@property (readonly, class) id<AMPUserNotificationCenterHandling> userNotificationCenterHandler;
```

返回一个委托 [AMPUserNotificationCenterHandling](https://boost.yandex.com/doc/zh/analytics/content/sdk/ios/push/objectivec/AMPUserNotificationCenterHandling.md)，用于手动处理 iOS 10 及更高版本的前台推送通知。

使用此代理，如果您用自定义逻辑实现了[UNUserNotificationCenterDelegate](https://developer.apple.com/documentation/usernotifications/unusernotificationcenterdelegate?language=objc)协议。 在这种情况下，您应该实现所有 `UNUserNotificationCenterDelegate` 代理的方法，并在 `AMPUserNotificationCenterHandling` 中调用适当的方法。

为了简化推送通知处理，使用[userNotificationCenterDelegate](#property_userNotificationCenterDelegate)。
