APN生产证书未被PushSharp识别

12

我开发了一个iOS应用程序,可以接收推送通知。我正在使用PushSharp从.NET环境中发送它们。在开发过程中一切都进展顺利,推送已成功发送:

var push = new PushBroker();
var appleCert = File.ReadAllBytes(@"Utils\Cert.Development.p12");
push.RegisterAppleService(new ApplePushChannelSettings(false, appleCert, "*******"));
push.QueueNotification(new AppleNotification()
    .ForDeviceToken(token)
    .WithContentAvailable(1)
);
push.StopAllServices();

现在,应用已经通过审核并在AppStore上发布。我已经生成了正确的生产证书:

var push = new PushBroker();
var appleCert = File.ReadAllBytes(@"Utils\Cert.Production.p12");
push.RegisterAppleService(new ApplePushChannelSettings(true, appleCert, "*******"));
push.QueueNotification(new AppleNotification()
    .ForDeviceToken(token)
    .WithContentAvailable(1)
);
push.StopAllServices();

但是当我尝试使用PushSharp发送推送时,它会抛出以下异常:

You have selected the Production server, yet your Certificate does not appear to be the Production certificate! Please check to ensure you have the correct certificate!

我相信我已经按照所有步骤操作了。我下载了与应用程序发布所设定的配置文件绑定的生产证书。我打开它并导出了.p12文件。

同时我确定我没有错误地使用开发证书,因为如果我将PushSharp设置为开发模式并使用最后一个证书,它会抛出以下错误:

You have selected the Development/Sandbox (Not production) server, yet your Certificate does not appear to be the Development/Sandbox (Not production) certificate! Please check to ensure you have the correct certificate!

证书既不是开发证书也不是生产证书,怎么可能呢?

有没有地方可以验证该文件?请给我一些关于这个问题的见解,因为我不知道从哪里开始。


我遇到了同样的问题。你能解决它吗?你尝试关闭证书检查了吗? - snowCrabs
这是哪个版本的PushSharp?2.X.X吗? - Adam Mendoza
9个回答

16

苹果已更改名称。请转到ApplePushChannelSettings.cs并按以下方式更改名称。

if (production && !subjectName.Contains("Apple Production IOS Push Services"))

if (production && !subjectName.Contains("Apple Push Services"))

昨天在更新我的过期证书时,我需要这样做。更改名称,重新构建并上传到服务器,然后它就能正常工作了。


你能指定那个文件的位置或者在哪里进行编辑吗?我正在运行 PushSharp 2.2.1,但是没有看到相关内容。 - cvocvo

4

我正在使用PushSharp 4.0.10版本,这正是解决我的问题的方法。谢谢! - Boris

4

苹果推出了新的通用推送证书,可以连接到APNs生产和开发环境。这就是为什么生产证书的公共名称从Apple Production IOS Push Services更改为Apple Push Services。

您应该更改提供程序推送服务器上的代码以与新的公共名称兼容。


3
问题出在PushSharp库中,只需将其更新到版本.3即可,原因是由于苹果已经将推送证书名称从(Apple生产推送服务)更改为(Apple推送服务) 而PushSharp会检查证书的名称: (生产&&!subjectName.Contains(“Apple Push Services”))。 请注意更新PushSharp库以解决此问题。

2

错误:您选择了生产服务器,
但是您的证书似乎不是生产证书!
请检查确保您有正确的证书!

解决方案:(production && !subjectName.Contains("Apple Push Services"))


2

调用以下代码段发送设备令牌和消息

 public void PendingNotification(string DeviceToken,string message)
    {
        try
        {
            int port = 2195;
            //Developer
            String hostname = "gateway.sandbox.push.apple.com";
            //Production
            //String hostname = "gateway.push.apple.com";
            String certificatePassword = "XXXXXX";
            string certificatePath = Server.MapPath("~/Cert.p12");
            TcpClient client = new TcpClient(hostname, port);
            X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), certificatePassword);
            X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);
            SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
            sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls, false);
            //String DeviceToken = "a5062b62aacbe6a499e02351c3f233ce87004574ff01965dff5f6bb8f15cae13";
            String LoginName = "Name";
            int Counter = 1; //Badge Count;  
            String Message = message;
            String UID = "your choice UID";
            string payload = "{\"aps\":{\"alert\":\"" + Message + "\",\"badge\":" + Counter + ",\"sound\":\"default\"},\"UID\":\"" + UID + "\",\"LoginName\":\"" + LoginName + "\"}";
            MemoryStream memoryStream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(memoryStream);
            writer.Write((byte)0);
            writer.Write((byte)0);
            writer.Write((byte)32);
            writer.Write(HexStringToByteArray(DeviceToken.ToUpper()));
            writer.Write((byte)0);
            writer.Write((byte)payload.Length);
            byte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload);
            writer.Write(b1);
            writer.Flush();
            byte[] array = memoryStream.ToArray();
            sslStream.Write(array);
        }
        catch (Exception ex)
        {
            //Response.Write(ex.Message);
        }
    }
    public static byte[] HexStringToByteArray(string hex)
    {
        return Enumerable.Range(0, hex.Length)
            .Where(x => x % 2 == 0)
            .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
            .ToArray();
    }
    public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
        if (sslPolicyErrors == SslPolicyErrors.None)
            return true;
        Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
        return false;
    }


0

我建议使用3.0 NuGet预览版本中的某个东西。这个问题在这些版本中已经修复,不会被移植到2.x。


1
好的,3.0版本正在测试阶段,APNS证书开始出现错误。如果能够在2.x版本中修复这个问题就太好了。更不用说库的重写了。想象一下现在依赖这个库的程序员和他们的实时服务。 - Kemal Taşkın

0
请按照以下链接中的步骤生成生产 SSL 证书:

如何创建 APNS 证书

如果这没有起作用,请仔细检查您的代码,确保您正在读取正确的证书文件,并且将 True 传递给 ApplePushChannelSettings.


0

PushSharp 2.x > Push.Apple > ApplePushChannelSettings.cs

在ApplePushChannelSettings.cs中找到DetectProduction()和CheckProductionCertificateMatching(),并将'.Contains("Apple Production IOS Push Services")'替换为'.Contains("Apple Push Services")'

这是因为苹果将证书名称从“Apple Production IOS Push Services”更改为“Apple Push Services”,PushSharp通过证书名称识别类型(生产/开发)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接