1

我们使用 Firebase 作为我们的后端,使用适用于 Android 和 iOS 的“Unity 云构建”服务来构建我的项目。最近我们决定在我们的项目中添加“推送通知”功能以增强用户体验。

我开始测试以了解整个事情是如何工作的,我添加了 firebase-messaging 包和标准的“官方”代码来接收和保存令牌。我还使用“Firebase 函数”制作了一些“发送者”代码来发送测试通知。

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

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

这是 iOS 版本的行为方式:

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

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

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

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

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);
    }
}

非常感谢。

4

1 回答 1

1

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

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

于 2019-07-11T08:46:51.050 回答