Neeraj Purohit
Neeraj Purohit

Reputation: 243

Pushsharp 4.0.10.0 ApnsConfiguration connection error for iOS device tokens

I am using PushSharp 4.0.10.0 library to send the notification on iOS devices but it's not working. I have debugged it and found there is some ApnsConfiguration connection problem.

I am using this code to send the notificaiton:

 public IHttpActionResult Notify()
   {
       HttpResponseMessage response = new HttpResponseMessage();
       HttpContent requestContent = Request.Content;
       string errorMessage = "Some error occured.please try again later";
       HttpStatusCode responseCode = HttpStatusCode.Unauthorized;
       string requestParameter = requestContent.ReadAsStringAsync().Result;
       string tokan = "";
       var r = Request;
       var header = r.Headers;
       try
       {
           if (requestParameter != null)
           {
               PushNotificationModel complaintModel = JsonConvert.DeserializeObject<PushNotificationModel>(requestParameter);
               JsonConvert.DeserializeObject<PushNotificationModel>(requestParameter);
               var appleCert = File.ReadAllBytes(HttpContext.Current.Server.MapPath("~/Images/User/xyz.pem"));
               var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Production,
                                appleCert, "xyz");

               // Create a new broker
               var push = new ApnsServiceBroker(config);

               int DeviceType = 1;
               string deviceId = Convert.ToString(complaintModel.deviceToken);
               string message = "New notification!!";
               Guid complaintId = complaintModel.ComplaintId;
               string detail = complaintModel.detail;
               try
               {
                   //System.Web.Hosting.HostingEnvironment.MapPath("~/Images/User/")
                  // var appleCert = File.ReadAllBytes(HttpContext.Current.Server.MapPath("~/Images/User/CTPwd.pem"));
                   push.OnNotificationFailed += (notification, aggregateEx) =>
                   {

                       aggregateEx.Handle(ex =>
                       {

                           // See what kind of exception it was to further diagnose
                           if (ex is ApnsNotificationException)
                           {

                               message = ex.Message;
                           }
                           else
                           {
                               message = "Not an APNSException";
                           }

                           // Mark it as handled
                           return true;
                       });
                   };
                   try
                   {
                       push.OnNotificationSucceeded += (notification) =>
                       {
                           message = "New Notification";
                       };
                       push.Start();
                       string appleJsonFormat = "{\"aps\": {\"alert\":" + '"' + message + '"' + ",\"sound\": \"default\"}}";
                       //string appleJsonFormat = "{\"aps\": {\"alert\": " + "Hello World" + ",\"sound\": \"default\"}}";
                       push.QueueNotification(new ApnsNotification
                       {
                           DeviceToken = deviceId,
                           Payload = JObject.Parse(appleJsonFormat)
                       });

                       push.Stop();
                   }
                   catch(Exception ex)
                   {

                   }

I have searched on google, but did not find any relevant answer. Is there any syntax problem ?

Thanks in advance.

Upvotes: 1

Views: 604

Answers (1)

Ranjeet Singh Rathore
Ranjeet Singh Rathore

Reputation: 45

Please use .P12 file format for push notification happy coding:)

Upvotes: 1

Related Questions