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

# 与其他推送服务配合使用

您可以同时使用AppMetrica Push SDK 和其他推送服务。 要完成这一步，你需要创建一个FCM或HMS服务，用于在集成的SDK之间转发消息。

## 使用其他 Firebase 推送服务 {#fcm}

### 步骤 1. 在 AndroidManifest.xml 中进行修改 {#fcm-settings}

在 `应用` 元素中修改 `AndroidManifest.xml` 文件：

```xml translate=no
<service android:name=".FirebaseMessagingMainService"
         android:enabled="true"
         android:exported="false">
    <intent-filter android:priority="100">
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>
<service android:name="io.appmetrica.analytics.push.provider.firebase.AppMetricaMessagingService" tools:node="remove"/>
```

### 步骤 2. 向项目添加依赖项 {#dependencies}

{% list tabs %}

- 应用/build.gradle.kts

   ```kotlin translate=no
   dependencies {
       implementation("io.appmetrica.analytics:push-provider-firebase:$appmetrica_push_version")
   }
   ```
- app/build.gradle

   ```groovy translate=no
   dependencies {
       implementation "io.appmetrica.analytics:push-provider-firebase:$appmetrica_push_version"
   }
   ```

{% endlist %}

### 步骤 3. 添加推送通知处理 {#handling}

声明派生类 `FirebaseMessagingMainService`，基于基础类 `FirebaseMessagingService`，用于处理推送通知：

{% list tabs group=instructions %}

- Kotlin

   ```kotlin translate=no
   class FirebaseMessagingMainService : FirebaseMessagingService() {
       override fun onMessageReceived(message: RemoteMessage) {
           super.onMessageReceived(message)
           if (AppMetricaMessagingService.isNotificationRelatedToSDK(message)) {
               AppMetricaMessagingService().processPush(this, message)
               return
           }

           // Implement the logic for sending messages to other SDKs or handle own pushes.
       }
   }
   ```
- Java

   ```java translate=no
   public class FirebaseMessagingMainService extends FirebaseMessagingService {
       @Override
       public void onMessageReceived(RemoteMessage message) {
           super.onMessageReceived(message);
           if (AppMetricaMessagingService.isNotificationRelatedToSDK(message)) {
               new AppMetricaMessagingService().processPush(this, message);
               return;
           }

           // Implement the logic for sending messages to other SDKs or handle own pushes.
       }
   }
   ```

{% endlist %}

### 第4步。 添加推送令牌处理 {#tokens}

将处理 push token 的代码添加到 `FirebaseMessagingMainService` 类中：

{% list tabs group=instructions %}

- Kotlin

   ```kotlin translate=no
   class FirebaseMessagingMainService : FirebaseMessagingService() {
       override fun onNewToken(token: String) {
           super.onNewToken(token);
           AppMetricaMessagingService().processToken(this, token)

           // Implement the logic for sending tokens to other SDKs.
       }
   }
   ```

- Java

   ```java translate=no
   public class FirebaseMessagingMainService extends FirebaseMessagingService {
       @Override
       public void onNewToken(@NonNull String token) {
           super.onNewToken(token);
           new AppMetricaMessagingService().processToken(this, token);

           // Implement the logic for sending tokens to other SDKs.
       }
   }
   ```

{% endlist %}

## 使用其他HMS推送服务 {#hms}

### 步骤 1. 在 AndroidManifest.xml 中进行修改 {#hms-settings}

在 `应用` 元素中修改 `AndroidManifest.xml` 文件：

```xml translate=no
<service
    android:name=".HmsMessagingMainService"
    android:exported="true"
    android:permission="${applicationId}.permission.PROCESS_PUSH_MSG">
    <intent-filter android:priority="100">
        <action android:name="com.huawei.push.action.MESSAGING_EVENT" />
    </intent-filter>
</service>
<service android:name="io.appmetrica.analytics.push.provider.hms.AppMetricaHmsMessagingService" tools:node="remove"/>
```

### 步骤 2. 添加推送通知处理 {#hms-handling}

从基础类 `HmsMessageService` 声明派生类 `HmsMessagingMainService`，用于处理推送通知：

{% list tabs group=instructions %}

- Kotlin

   ```kotlin translate=no
   class HmsMessagingMainService : HmsMessageService() {
       override fun onMessageReceived(message: RemoteMessage) {
           super.onMessageReceived(message)
           if (AppMetricaHmsMessagingService.isNotificationRelatedToSDK(message) {
               AppMetricaHmsMessagingService().processPush(this, message)
               return
           }

           // Implement the logic for sending messages to other SDKs or handle own pushes.
       }
   }
   ```

- Java

   ```java translate=no
   public class HmsMessagingMainService extends HmsMessageService {
       @Override
       public void onMessageReceived(RemoteMessage message) {
           super.onMessageReceived(message);
           if (AppMetricaHmsMessagingService.isNotificationRelatedToSDK(message) {
               new AppMetricaHmsMessagingService().processPush(this, message);
               return;
           }

           // Implement the logic for sending messages to other SDKs or handle own pushes.
       }
   }
   ```

{% endlist %}

### 步骤 3. 添加推送令牌处理 {#hms-tokens}

将 push token 处理添加到 `HmsMessagingMainService` 类代码中：

{% list tabs group=instructions %}

- Kotlin

   ```kotlin translate=no
   class HmsMessagingMainService : HmsMessageService() {
       override fun onNewToken(token: String?) {
           super.onNewToken(token);
           AppMetricaHmsMessagingService().processToken(this, token)

           // Implement the logic for sending tokens to other SDKs.
       }
   }
   ```

- Java

   ```java translate=no
   public class HmsMessagingMainService extends HmsMessageService {
       @Override
       public void onNewToken(@Nullable String token) {
           super.onNewToken(token);
           new AppMetricaHmsMessagingService().processToken(this, token);

           // Implement the logic for sending tokens to other SDKs.
       }
   }
   ```

{% endlist %}

#### 另请参见 {#see-also}

- [连接 AppMetrica Push SDK](https://boost.yandex.com/doc/zh/analytics/content/sdk/android/push/quick-start.md)
- [启动推送营销](https://boost.yandex.com/doc/zh/analytics/content/push/marketing.md)

<!-- source: zh/analytics/content/_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: zh/analytics/content/_includes/feedback-button-3.md -->
