2016-10-05 40 views
0

我们使用私钥唯一选项创建了证书.p12。我们尝试通过MAC进行推送通知,并且工作正常。我用下面的代码没有给出任何错误,但iPhone没有收到任何通知。在这个帮手是我的班级写日志文件。推送通知以使IOS不能正常工作

public void PushNotificationIOS(string message, string registrationKey, int type) 
    { 
     string deviceID = registrationKey; 
     int port = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["IosPort"]); //2195; 
     string hostname = System.Configuration.ConfigurationManager.AppSettings["HostName"];//"gateway.sandbox.push.apple.com"; 
     string certPath = string.Empty; 
     certPath = System.Configuration.ConfigurationManager.AppSettings["CertificatePath"] + System.Configuration.ConfigurationManager.AppSettings["CertificateName"];//"~/Content/PushCertificateNew.p12";    
     string certificatePath = System.Web.Hosting.HostingEnvironment.MapPath(certPath); 
     string certPassword = System.Configuration.ConfigurationManager.AppSettings["CertificatePassword"]; 
     TcpClient client = new TcpClient(hostname, port); 
     try 
     { 
      X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), certPassword); 
      X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate); 
      SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null); 

      try 
      { 
       sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls, false); 
      } 
      catch (Exception ex) 
      { 
       Helper.WriteLog("sslStream.AuthenticateAsClient : TLS " + ex.Message); 
      } 

      MemoryStream memoryStream = new MemoryStream(); 
      BinaryWriter writer = new BinaryWriter(memoryStream); 
      writer.Write((byte)0); 
      writer.Write((byte)0); 
      writer.Write((byte)32); 
      writer.Write(StringToByteArray(deviceID.ToUpper())); 
      String payload = "{\"aps\":{\"alert\":\"" + message + "\",\"badge\":0,\"sound\":\"default\"}}"; 
      writer.Write((byte)0); 
      writer.Write((byte)payload.Length); 
      byte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload); 
      writer.Write(b1); 
      writer.Flush(); 
      byte[] array = memoryStream.ToArray(); 
      sslStream.Write(array); 
      sslStream.Flush(); 
      client.Close(); 
     } 
     catch (System.Security.Authentication.AuthenticationException ex) 
     { 
      Helper.WriteLog("PushNotificationIOS catch I :" + ex.Message); 
      client.Close(); 
     } 
     catch (Exception e) 
     { 
      Helper.WriteLog("PushNotificationIOS catch II :" + e.Message); 
      client.Close(); 
     } 

    } 

谁能告诉我们如何追查问题?

回答

0

我认为问题出在KeyChain的.p12创建过程中。

您可以选择证书,并打开箭头以选择私钥并将它们一起导出为来自钥匙串访问的.p12文件。

enter image description here

从,你可以在终端

cd 
cd Desktop 
openssl pkcs12 -in pushcert.p12 -out pushcert.pem -nodes -clcerts 

使用以下命令生成的.pem文件你可以参考this的细节信息

+0

如果我们证明这样的比“sslStream.AuthenticateAsClient(hostname,certificatesCollection,SslProtocols.Tls,false)”提供错误“提供给软件包的凭据无法识别”;“。我发现只有在使用c#的情况下才能使用私钥创建证书。如果我们正在使用PHP进行编码,我们可以同时使用它们 –

+0

嗯好吧...我试图追查你的问题 –

+0

你发现了什么? –