【问题标题】:Pushsharp send apple notification failed : SSL Stream Failed to Authenticate as ClientPushsharp 发送苹果通知失败:SSL 流无法作为客户端进行身份验证
【发布时间】:2018-06-29 13:08:33
【问题描述】:

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

我的代码:

 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 内部的问题或缺少配置必须在我的机器上完成,有人知道吗?

【问题讨论】:

    标签: c# ios notifications pushsharp


    【解决方案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(master 分支)查看最新的代码,构建它,然后在我的项目中手动添加对构建的 DLL 的引用。

    之前我使用的是 NuGet PushSharp 包,现在已经 3 年了,还没有更新。如果您查看 master 分支上的最近提交,就会发现那里有一些与 Apple 和 TLS 相关的更改,所以我确信这已经修复了它。

    【讨论】:

    • 我遇到了完全相同的问题,来自 GitHub 的最新 PushSharp 也为我解决了这个问题,
    • 您提到的日期和 PushSharp 的更新为我指明了正确的方向,谢谢! 18 年 1 月的此提交中的更改为我修复了它:github.com/Redth/PushSharp/commit/…
    • 最近的更改已在新的 NuGet 包 ID 下发布:LogicSoftware.PushSharp。相同的作者,版本号从他们离开的地方开始。 nuget.org/packages/LogicSoftware.PushSharp
    【解决方案2】:

    我的问题是通过使用以下命令从 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】:

      我认为问题与 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);                   
      

      希望对你有帮助

      【讨论】:

      • 我下载了pushsharp 4的源码,找不到上面提到的类,反正我发现源码apnsconnection.cs中已经用到了tls
      猜你喜欢
      • 2019-01-08
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      • 2019-06-27
      • 1970-01-01
      • 2022-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多