Manel Pi

Manel Pi

  • 1.9k
  • 12
  • 5.8k

APNS using PushSharp not working

Sep 19 2017 2:22 AM
I'm getting the following error message trying to send a push notification to my iPhone:
 
Apple Notification Failed: ID={12}, Code= {PushSharp.Apple.ApnsConnectionException: SSL Stream Failed to Authenticate as Client---> System.Security.Authentication.AuthenticationException: Could not make a call to SSPI; see the internal exception. ---> System.ComponentModel.Win32Exception: Message received unexpectedly, or its format is incorrect}
 
I'm using PushSharp library and the following code:
  1. using PushSharp.Apple;   
  2. using PushSharp.Core;   
  3. using Newtonsoft.Json.Linq;    
  4. string p12fileName = "C:\\webroot\\PKI\\myCertificate.p12";   
  5. string p12password = "myPassword";   
  6. string deviceToken = "myiPhoneToken";    
  7. var appleCert = System.IO.File.ReadAllBytes(p12fileName);   
  8. var config = new PushSharp.Apple.ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Production, appleCert, p12password);   
  9. config.ValidateServerCertificate = false;   
  10. var apnsBroker = new ApnsServiceBroker(config);    
  11. apnsBroker.OnNotificationFailed += (notification, aggregateEx) => {       
  12.    aggregateEx.Handle(ex => {           
  13.       // See what kind of exception it was to further diagnose           
  14.       if (ex is ApnsNotificationException)           
  15.       {               
  16.          var notificationException = (ApnsNotificationException)ex;  
  17.          // Deal with the failed notification               
  18.          var apnsNotification = notificationException.Notification;               
  19.          var statusCode = notificationException.ErrorStatusCode;               
  20.          Response.Write("Apple Notification Failed: ID={" + apnsNotification.Identifier + "}, Code={" + statusCode + "}");  
  21.       }           
  22.       else           
  23.       {               
  24.          // Inner exception might hold more useful information like an ApnsConnectionException               
  25.          Response.Write("Notification Failed for some unknown reason : {" + ex.InnerException + "}");               
  26.       }           
  27.       // Mark it as handled           
  28.       return true;       
  29.    });   
  30. };   
  31. apnsBroker.OnNotificationSucceeded += (notification) => {       
  32.    Response.Write("Apple Notification Sent!");   
  33. };    
  34. apnsBroker.Start();   
  35. apnsBroker.QueueNotification(new ApnsNotification   
  36. {       
  37.    DeviceToken = deviceToken,       
  38.    Payload = JObject.Parse("{\"aps\":{\"alert\":\"" + "Hi,, This Is a Sample Push Notification For IPhone.." + "\",\"badge\":1,\"sound\":\"default\"}}")   
  39. });   
  40. apnsBroker.Stop();  
But if I try the same data at website http://pushtry.com/ I can get the push notification.
 
I created the certificate more than once trying to avoid any error at create certificate moment.
 
What I'm doing wrong? What is missing in my code?
 
Can anyone help me?
 
Kind regards in advance. 

Answers (2)