【问题标题】:APN Production certificate not being recognized by PushSharpPushSharp 不认可 APN 生产证书
【发布时间】:2015-04-14 11:35:22
【问题描述】:

我开发了一个接收推送通知的 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!

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

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

【问题讨论】:

  • 我遇到了同样的问题。你能在哪里让它工作?您是否尝试过关闭证书检查?
  • 这是什么版本的 PushSharp? 2.X.X?

标签: ios certificate apple-push-notifications pushsharp


【解决方案1】:

Apple 已更改名称。请前往 ApplePushChannelSettings.cs 并更改名称如下。

来自

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

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

我昨天更新过期证书时需要这样做。更改名称,重建并上传到服务器,然后它又可以工作了。

【讨论】:

  • 您能指定该文件的存在位置或编辑位置吗?我正在运行 PushSharp 2.2.1,但在任何地方都没有看到。
【解决方案2】:

Apple 推出了一种新的通用推送证书,可以连接到 APNs 生产和开发环境。这就是生产证书通用名称从 Apple Production IOS Push Services 更改为 Apple Push Services 的原因。

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

【讨论】:

    【解决方案3】:

    当您为 .NET 创建生产证书 (.p12) 时,始终像仅选择证书一样导出。见附图

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

    【讨论】:

    • 我使用的是 PushSharp 4.0.10 版,这正是为我解决了这个问题的原因。谢谢!
    【解决方案4】:

    问题出现在 PushSharp 库中,只需将其更新到版本 .3 这是因为苹果已将推送证书名称从(Apple 生产推送服务)更改为(Apple 推送服务) 并 pushSharp 检查证书的名称: (生产 && !subjectName.Contains("Apple Push Services"))。

    【讨论】:

      【解决方案5】:

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

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

      【讨论】:

        【解决方案6】:

        调用以下 sn-p 发送设备令牌和消息

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

        【讨论】:

          【解决方案7】:

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

          How To Create APNS Certificate

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

          【讨论】:

            【解决方案8】:

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

            【讨论】:

            • 嗯,3.0 处于测试阶段,APNS 证书开始出现错误。最好在 2.x 中进行此修复。更不用说,图书馆的重写。想象一下现在依赖这个库的程序员和他们的实时服务。
            【解决方案9】:

            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通过证书名称识别类型(生产/开发)。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2016-05-26
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多