如何解决 'Failed to register for remote notifications',在 'Unity Cloud Build' 构建的 iOS 游戏上接收推送通知

How to solve 'Failed to register for remote notifications', to receive push notifications on an iOS game that has built by 'Unity Cloud Build'

我们使用 "Unity cloud build" 服务为 Android 和 iOS 构建我的项目,使用 Firebase 作为我们的后端。最近我们决定在我们的项目中添加 "push notification" 功能以增强用户体验。

我开始测试以查看整个过程如何工作,我添加了 firebase-messaging 包和标准 "official" 代码来接收和保存令牌。我还使用 "Firebase functions" 编写了一些 "sender" 代码来发送测试通知。

Android 构建没有问题,它立即运行。它收到了令牌,每当我使用该令牌发送测试通知时,通知都会立即弹出,没有问题。

但是 iOS 版本有些问题!它不起作用!问题出在哪里非常模糊,我无法使用 Google 找到任何类似的问题!!我们花了很多时间来调整 .p12 和配置文件和 .p8 文件(用于 APN 与 firebase 的连接),它们看起来都很好,我们不确定问题是否存在!

这是 iOS 版本的行为方式:

  1. 日志显示设备已连接到 FCM 并收到令牌,但在那一行之后,始终有一条错误行:Failed to register for remote notifications

  2. 当我向该令牌发送消息时,它没有显示任何通知,仅在日志中,我可以看到:FCM: Received message

此外,这是我在 Unity 上用于启用远程通知的 iOS 设置: Image

这是我在 Unity 云构建上的 post-构建脚本

using UnityEngine;
using UnityEditor;
using UnityEditor.Callbacks;
using System.Collections;
using UnityEditor.iOS.Xcode;
using System.IO;

public class BuildSettings
{
    [PostProcessBuild]
    public static void OnPostprocessBuild(BuildTarget buildTarget, string path)
    {
        if (buildTarget != BuildTarget.iOS)
            return;
        string projectPath = path + "/Unity-iPhone.xcodeproj/project.pbxproj";
        PBXProject project = new PBXProject();
        project.ReadFromFile (projectPath);
        string targetGuid = project.TargetGuidByName(PBXProject.GetUnityTargetName());
        project.AddCapability(targetGuid, PBXCapabilityType.PushNotifications);
        project.AddFrameworkToProject(targetGuid, "CoreBluetooth.framework", false);
        project.AddFrameworkToProject(targetGuid, "UserNotifications.framework", false);
        File.WriteAllText (projectPath, project.WriteToString ());

        var plistPath = Path.Combine(path, "Info.plist");
        var plist = new PlistDocument();
        plist.ReadFromFile(plistPath);
        plist.root.SetString("NSBluetoothPeripheralUsageDescription", "Bluetooth connection message!");
        plist.WriteToFile(plistPath);
    }
}

非常感谢。

进一步调查显示问题出现在缺少 aps-environment 授权,如果您使用 Xcode 或云构建,将此授权添加到您的 iOS 构建的过程是不同的.

您可以在这里找到解决方案:https://forum.unity.com/threads/how-to-put-ios-entitlements-file-in-a-unity-project.442277/