我们使用仅私钥选项创建了证书.p12。我们尝试通过MAC推送通知及其工作正常。我使用下面的代码,没有给出任何错误,但iPhone没有收到任何通知。在这个Helper中是我写日志到文件的类。
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 :(得分:0)
我认为问题出在KeyChain的.p12创建过程中。
您可以选择证书,然后打开箭头以选择私钥并将其作为 .p12 文件从Keychain Access一起导出。
您可以使用终端
中的以下命令生成 .pem 文件cd
cd Desktop
openssl pkcs12 -in pushcert.p12 -out pushcert.pem -nodes -clcerts
您可以参考this了解详细信息