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

时间:2018-01-20 11:25:36

标签: c# ios notifications pushsharp

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

我的代码:

 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 个答案:

答案 0 :(得分:1)

这是我自2019年7月23日起发生的事情。看来Apple现在正在针对

的沙盒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年了,还没有更新。如果您查看master分支上的最近提交,则其中有一些与Apple和TLS相关的更改,因此,我确定这已经解决了该问题。

答案 1 :(得分:0)

我认为这个问题与Push Sharp有关,所以请通过在名为 ApplePushChannel.cs 的类中将SSl3更改为Tls来尝试此解决方案 这是改变

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

希望这会打败你

答案 2 :(得分:0)

通过使用以下命令从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