# 故障排除技巧

> Resolve common issues with Firebase settings, dependencies, and SDK integration.

如果在使用 Push Notifications SDK 时遇到任何问题或错误，应参考以下常见的故障排除技巧。

有些故障排除技巧可能会提及特定的常见错误。但是，即使您没有遇到这些问题，仍然建议您按顺序查看。

## 1.(Android) 在 Push Notifications（推送通知）设置中验证您的 Firebase 值##1-android-verify-your-firebase-values-inside-the-push-notifications-settings

在使用 Push Notification SDK 期间，如果您尝试将测试设备注册到 Firebase 时遇到问题，可能是因为您的 Firebase 值为空或无效。

您可以在 [Push Notifications Settings（推送通知设置）](./sdk.md#push-notifications-settings)细分段中查看如何添加 Firebase 详细信息。

或者，如果您确定提供的值正确无误，请确认值的后面没有**空格/空白**。这是一种导致问题的已知原因。

## 2.(EDM4U/MDR) 验证是否已解析 Push Notifications SDK 的依赖项##2-edm4umdr-verify-that-the-dependencies-for-the-push-notifications-sdk-have-been-resolved

> **Note:**
>
> 适用于将 External Dependency Manager for Unity (EDM4U) 或 Mobile Dependency Resolver (MDR) 与 Push Notifications 集成在一起的用户。

如 [External Dependency Manager for Unity (EDM4U)](./sdk.md#external-dependency-manager-for-unity-edm4u-support) 支持部分所述，Push Notifications SDK 不需要也不会捆绑 External Dependency Manager for Unity (EDM4U) 或 Mobile Dependency Resolver (MDR) 来解析其自己的依赖项。但是，如果使用了这两者之中的任何一个，则 Push Notifications SDK 会与它们集成在一起。

如果任何其他包也不需要 EDM4U 或 MDR，**建议卸载它们**。

否则，我们建议您按照以下步骤操作：

1. 确认已生成依赖项文件 `PushSDKDependencies.xml`。将此文件放在自动生成的 `Assets/Push Notifications/Editor/Android` 目录下。
   * 如果此文件不存在，请重新打开您的 Unity 项目。在这个过程期间应该会生成此文件。
2. 使用 `Resolve` 或 `Force Resolve` 选项，这两个选项都位于 **Assets（资源）> External Dependency Manager > Android Manager** 下。
3. 使用 `Display Libraries` 选项确认已解析 Push Notifications SDK 的依赖项，该选项也位于 **Assets（资源）> External Dependency Manager > Android Manager** 下。应该会显示以下行：
   ```text
   implementation 'com.google.firebase:firebase-messaging-ktx:22.0.0' // Assets/Push Notifications/Editor/Android/PushSDKDependencies.xml:9
   ```
   * 请注意，如果其他包或 SDK 使用同样的依赖项，则以上行旁边的注释可能略有不同。


**Frame:**
![](/api/media?file=/push-notifications/media/images/edm4u-options.png)

## 3.(Android) 确认 Minify（缩小）功能在构建过程中未排除 Push Notifications SDK##3-android-check-that-minify-isnt-excluding-the-push-notifications-sdk-during-the-build-process

如果您已完成 [EDM4U/MDR 故障排除部分](#2-edm4umdr-verify-that-the-dependencies-for-the-push-notifications-sdk-have-been-resolved)，但仍然收到类似于以下错误的信息：

```text
java.lang.ClassNotFoundException: com.unity.services.pushnotifications.android.UnityCallbackClass
```

检查 **Player Settings（播放器设置）> Publishing（发布）> Minify（缩小）** 下的 [Minify（缩小）](https://docs.unity3d.com/Manual/class-PlayerSettingsAndroid.html#minify)选项。如果勾选了 `Release` 或 `Debug` 复选框，则表示会使用 Minify（缩小），这种情况下可能会在缩小过程从发布/调试构建中移除 Push Notifications SDK，因此会导致“class missing（类缺失）”错误。

如果您不需要 Minify（缩小），可以将其关闭来解决这一问题。

如果要继续使用 Minify（缩小），则需要在[自定义 proguard 文件](https://docs.unity3d.com/Manual/class-PlayerSettingsAndroid.html#build)下添加 Push Notifications 类，以防止在缩小过程中排除 Push Notifications SDK 功能。

在 **Player Settings（播放器设置）> Publishing（发布）> Build（构建）> Custom Proguard File（自定义 Proguard 文件）** 下启用 `Custom Proguard File`。


**img.png:**
![img.png](/api/media?file=/push-notifications/media/images/custom-proguard.png)

打开在指定路径下创建的文件，并附加以下行：

```text
-keep class com.unity.services.pushnotifications** { *;}
```

这样应该可以解决问题。

## 4. (iOS) 确保在 XCODE 中启用远程通知##4-ios-ensure-remote-notifications-are-enabled-in-xcode

为了使 iOS 应用能够接收推送通知，需要授予 `Remote Notifications` 功能。

如果不执行此操作，您的应用将出现以下错误：

```text
Failed to register for remote notifications: no valid “aps-environment” entitlement string found for application
```

转到 **Signing & Capabilities（签名和功能）> Capability（功能）> Background Modes（后台模式）> Remote Notifications（远程通知）** 以提供 `Remote Notifications` 功能。


**Frame:**
![](/api/media?file=/push-notifications/media/images/xcode-build.png)

## 5.在 Unity Dashboard（Unity 后台）的 Push Notifications（推送通知）设置下检查您的 API 密钥##5-check-your-api-keys-in-the-unity-dashboard-under-push-notifications-settings

要向终端用户设备发送推送通知，请将您的 Google 密钥和 Apple 密钥添加到 Unity Dashboard（Unity 后台）的 `Settings` 中，位于 **LiveOps > Push Notifications** 下。

您可以在[此处](./get-started.md#google)找到添加 Google 密钥的指南，在[此处](./get-started.md#apple)找到添加 Apple 密钥的指南。

对于 **iOS**，请确保您的密钥与目标构建版本匹配。对于**调试**构建，请将 `Sandbox` 选项设置为 `True`，而对于**生产**构建，请将其设置为 `False`。如果密钥和构建版本不匹配，测试设备将不会收到通知。

> **Note:**
>
> 从生成的 XCODE 项目构建 iOS 应用时，可能需要将 `run` 部分中每个目标（例如，`Unity-iPhone`、`notificationservice`）的方案从 `debug` 切换到 `release`（如果 `Sandbox` 设置为 `False`），或从 `release` 切换到 `debug`（如果 `Sandbox` 设置为 `True`）。

按照[测试指南](./sdk.md#testing-the-sdk-integration)检查密钥是否有效。
