代码之家  ›  专栏  ›  技术社区  ›  Mohammad Ghanem

Pushsharp发送apple通知失败:SSL流无法作为客户端进行身份验证

  •  3
  • Mohammad Ghanem  · 技术社区  · 7 年前

    我试图使用ASP上的Pushsharp库向apple设备发送推送通知。NET MVC项目托管在IIS上。

    我的代码:

     public static void SendAppleNotification()
            {
                // Configuration (NOTE: .pfx can also be used here)
                byte[] arr = File.ReadAllBytes("D:\\MySoftware\\pa_Dev.pem");
    
                var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox,
         arr, "1234");
    
                // Create a new broker
                var apnsBroker = new ApnsServiceBroker(config);
    
                // Wire up events
                apnsBroker.OnNotificationFailed += (notification, aggregateEx) => {
    
                    aggregateEx.Handle(ex => {
    
                        // See what kind of exception it was to further diagnose
                        if (ex is ApnsNotificationException)
                        {
                            var notificationException = (ApnsNotificationException)ex;
    
                            // Deal with the failed notification
                            var apnsNotification = notificationException.Notification;
                            var statusCode = notificationException.ErrorStatusCode;
    
                            Console.WriteLine($"Apple Notification Failed: ID={apnsNotification.Identifier}, Code={statusCode}");
    
                        }
                        else
                        {
                            // Inner exception might hold more useful information like an ApnsConnectionException           
                            Console.WriteLine($"Apple Notification Failed for some unknown reason : {ex.InnerException}");
                        }
    
                        // Mark it as handled
                        return true;
                    });
                };
    
                apnsBroker.OnNotificationSucceeded += (notification) => {
                    Console.WriteLine("Apple Notification Sent!");
                };
    
                // Start the broker
                apnsBroker.Start();
    
    
                    // Queue a notification to send
                    apnsBroker.QueueNotification(new ApnsNotification
                    {
                        DeviceToken = "660E4433785EFF2B2AA29D5076B039C969F1AADD839D79261328F40B08D26497",
                        Payload = JObject.Parse("{\"aps\":{\"badge\":7}}")
                    });
    
    
                // Stop the broker, wait for it to finish   
                // This isn't done after every message, but after you're
                // done with the broker
                apnsBroker.Stop();
    
    
            }
    

    笔记: 1-试图将pem扩展更改为p12,但仍然出现相同的问题。 2-我尝试使用发送推送通知 https://pushtry.com/ 并且其工作正常,所以不会从证书文件或密码中发出。

    pushsharp内部的问题或缺少配置必须在我的机器上完成,有人知道吗?

    3 回复  |  直到 7 年前
        1
  •  4
  •   dodgy_coder    5 年前

    截至2019年7月23日,这种情况刚刚发生在我身上。看起来苹果现在正在为位于的沙盒voip推送通知服务器实施TLS 1.2

    gateway.sandbox.push.apple.com - port 2195
    feedback.sandbox.push.apple.com - port 2196
    

    我发现我必须从 https://github.com/Redth/PushSharp (主分支),构建它,然后在我的项目中手动添加对已构建DLL的引用。

    此前,我正在酝酿NuGet PushSharp软件包,该软件包现已使用3年,尚未更新。如果您查看主分支上最近的提交,就会发现与Apple和TLS相关的一些更改,因此我确信这已经修复了它。

        2
  •  1
  •   Mohammad Ghanem    5 年前

    我的问题通过使用以下命令从pem生成p12文件而不是重命名文件扩展名来修复。

    openssl pkcs12 -export -inkey sofwareKey.pem -in software_Prod.pem -out cert_key.p12
    

    查看更多

    https://www.paypal.com/us/selfhelp/article/how-do-i-convert-my-pem-format-certificate-to-pkcs12-as-required-by-the-java-and-.net-sdks-ts1020

    可能对任何人都有帮助。

        3
  •  0
  •   Ahmad Nasser    7 年前

    我认为这个问题与Push Sharp有关,所以请在名为 ApplePushChannel。反恐精英 这是零钱

      The orginal code in the file is 
      stream.AuthenticateAsClient(this.appleSettings.Host, this.certificates, System.Security.Authentication.SslProtocols.Ssl3, false);
    
     replace it with 
    
     stream.AuthenticateAsClient(this.appleSettings.Host, this.certificates, System.Security.Authentication.SslProtocols.Tls, false);                   
    

    希望这会让你失望