OGeek|极客世界-中国程序员成长平台

标题: c# - Pushsharp 发送苹果通知失败 : SSL Stream Failed to Authenticate as Client [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:54
标题: c# - Pushsharp 发送苹果通知失败 : SSL Stream Failed to Authenticate as Client

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



Best Answer-推荐答案


截至 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 相关的更改,所以我确信这已经修复了它。

关于c# - Pushsharp 发送苹果通知失败 : SSL Stream Failed to Authenticate as Client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48355550/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (https://ogeek.cn/) Powered by Discuz! X3.4