PushSharp无法识别APN生产证书

时间:2015-02-13 12:13:11

标签: ios certificate apple-push-notifications pushsharp

我开发了一款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!

证书如何既不是开发,也不是生产?

我可以在哪里验证文件吗?请给我一些关于此事的意见,因为我不知道从哪里开始

9 个答案:

答案 0 :(得分:15)

Apple改名了。请转到ApplePushChannelSettings.cs并更改名称,如下所示。

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

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

我昨天更新过期的证书时需要这样做。更改名称,重建名称并上传到服务器,然后再次运行。

答案 1 :(得分:4)

Apple推出了一种新的通用推送证书,可以连接到APN的生产和开发环境。这就是为什么生产证书的通用名称已从Apple Production IOS推送服务更改为Apple推送服务。

您应该更改提供者推送服务器上的代码以与新的通用名称兼容。

答案 2 :(得分:3)

为.NET创建生产证书(.p12)时,请始终导出,例如仅选择证书。见附图

http://davidbits.blogspot.in/2016/02/error-you-have-selected-production.html

enter image description here

答案 3 :(得分:3)

问题出现在PushSharp Library中,只是将其更新为Version .3这是因为Apple已将推送证书名称从(Apple Production Push Service)更改为(Apple Push Service) 和pushSharp检查证书的名称: (production&&!subjectName.Contains(“Apple推送服务”))。

答案 4 :(得分:2)

  

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

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

答案 5 :(得分:1)

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

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

答案 6 :(得分:0)

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

How To Create APNS Certificate

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

答案 7 :(得分:0)

我建议使用3.0 nuget预览版中的内容。此问题在这些版本中已得到修复,不会向后移植到2.x。

答案 8 :(得分:0)

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

在ApplePushChannelSettings.cs上找到DetectProduction()和CheckProductionCertificateMatching()并将'.Contains(“Apple Production IOS推送服务”)'替换为'。'包含(“Apple推送服务”)'

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