title: Apple Notification Failed: ID=2, Code=ConnectionError
body: <p>I'm using PushSharp 4.0.4, installed from NuGet</p>
  
  <p>In the OnNotificationFailed(ApnsNotification, AggregateException) event of the Apns broker, I often get this exception: </p>
  
  <blockquote>
    <p>Apple Notification Failed: ID=2, Code=ConnectionError .</p>
  </blockquote>
  
  <p>According to me it appears due to p12 file. It may not have all the right to access by the external API.</p>
  
  <pre><code>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) =&gt;
          {
              aggregateEx.Handle(ex =&gt;
              {
                  // 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) =&gt;
          {
              lblStatus.Text = "Apple Notification Sent successfully!";
          };
  
          var fbs = new FeedbackService(config);
          fbs.FeedbackReceived += (string devicToken, DateTime timestamp) =&gt;
          {
              // 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;
      }
  }
  </code></pre>