Rohit Goyat
Rohit Goyat

Reputation: 69

Apple Notification Failed: ID=2, Code=ConnectionError

I'm using PushSharp 4.0.4, installed from NuGet

In the OnNotificationFailed(ApnsNotification, AggregateException) event of the Apns broker, I often get this exception:

Apple Notification Failed: ID=2, Code=ConnectionError .

According to me it appears due to p12 file. It may not have all the right to access by the external 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;
    }
}

Upvotes: 1

Views: 944

Answers (1)

user11662710
user11662710

Reputation:

I'm using PushSharp 4.0.4, installed from NuGet .

To be run APNS push notification in C#.I got an error:

Apple Notification Failed: ID=1, Code=ConnectionError

Solution:

In this Error to Export key chain private key certificate .p12 format and place the certificate try again.To be get the out put.

Upvotes: 0

Related Questions