Apple 通知失败:ID=2,代码=ConnectionError

Posted

技术标签:

【中文标题】Apple 通知失败:ID=2,代码=ConnectionError【英文标题】:Apple Notification Failed: ID=2, Code=ConnectionError 【发布时间】:2017-12-12 20:51:40 【问题描述】:

我使用的是从 NuGet 安装的 PushSharp 4.0.4

在Apns broker的OnNotificationFailed(ApnsNotification, AggregateException)事件中,我经常会得到这个异常:

据我所知,它似乎是由于 p12 文件。它可能没有外部 API 的所有访问权限。

private void SendPushNotification(string deviceToken, string message)

    try
    
        //Get Certificate
        var appleCert = System.IO.File.ReadAllBytes(HttpContext.Current.Server.MapPath("Certificates.p12"));

        //Configuration(NOTE: .pfx can also be used here)
        var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox ,appleCert, "1234567890");

        //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;
                    string desc = $"Apple Notification Failed: ID=apnsNotification.Identifier, Code=statusCode";
                    Console.WriteLine(desc);
                    lblStatus.Text = desc;
                
                else
                
                    string desc = $"Apple Notification Failed for some unknown reason : ex.InnerException";
                    // Inner exception might hold more useful information like an ApnsConnectionException           
                    Console.WriteLine(desc);
                    lblStatus.Text = desc;
                

                // Mark it as handled
                return true;
            );
        ;

        apnsBroker.OnNotificationSucceeded += (notification) =>
        
            lblStatus.Text = "Apple Notification Sent successfully!";
        ;

        var fbs = new FeedbackService(config);
        fbs.FeedbackReceived += (string devicToken, DateTime timestamp) =>
        
            // Remove the deviceToken from your database
            // timestamp is the time the token was reported as expired
        ;

        //Start Proccess
        apnsBroker.Start();

        if (deviceToken != "")
        
            apnsBroker.QueueNotification(new ApnsNotification
            
                DeviceToken = deviceToken,
                Payload = JObject.Parse(("\"aps\":\"badge\":1,\"sound\":\"oven.caf\",\"alert\":\"" + (message + "\"")))
            );
        

        apnsBroker.Stop();

    
    catch (Exception)
    

        throw;
    

【问题讨论】:

您好,欢迎来到 ***。如果您能提供更多关于您的编程问题的功能和背景的上下文,那就太好了,但最重要的是,您需要询问question。 这都是关于后台编程的。我没有使用过这样的后台编程功能,如果有什么需要请指定。谢谢 【参考方案1】:

我使用的是从 NuGet 安装的 PushSharp 4.0.4。

要在 C# 中运行 APNS 推送通知。我收到一个错误:

Apple 通知失败:ID=1,代码=ConnectionError

解决方案:

在此错误中导出密钥链私钥证书 .p12 格式并放置证书重试。得到输出。

【讨论】:

以上是关于Apple 通知失败:ID=2,代码=ConnectionError的主要内容,如果未能解决你的问题,请参考以下文章

AWS - SNS 端点为 iOS 自行禁用 - Apple 设备(推送通知失败)

Qt 5.8 Apple 通知服务器握手失败

更改Apple ID后离子推送通知不起作用

Apple推送通知有时但并非总是有效

Apple Mach-O链接器(id)错误 - 链接器命令失败,退出代码为1(使用-v查看调用)C ++ SFML

XCode - 启用推送通知需要已添加 Apple ID