Push Notification is working fine when i use php server using .pem
file but when i use .p12
file for.net
server it is not working and also i checked locally using APN Tester it works great.
i can't understand where is the problem at my side or .net
server side. Please help me out.
this is a code which .net developer are using.
{
Helper.WriteLog("Message :" + message);
string deviceID = registrationKey; //"5712330e5fsdf2870c972d5c598ece88efbdac31baceed4961c5f4c53e9123594132";
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.HttpContext.Current.Server.MapPath(certPath);
Helper.WriteLog("PushNotificationIOS certificatePath:" + certificatePath);
string certPassword = System.Configuration.ConfigurationManager.AppSettings["CertificatePassword"];
//String certificatePath = @"C:\inetpub\ftproot\HQ\Content\PushCertificateNew.p12"; //System.Web.HttpContext.Current.Server.MapPath(certPath);
TcpClient client = new TcpClient(hostname, port);
Helper.WriteLog("Message :" + message);
Helper.WriteLog(":" + deviceID);
Helper.WriteLog("client after");
try
{
X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), certPassword);
Helper.WriteLog("clientCertificate after");
X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);
Helper.WriteLog("certificatesCollection after");
SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
Helper.WriteLog("sslStream after");
try
{
sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls, false);
}
catch (Exception ex)
{
Helper.WriteLog("sslStream.AuthenticateAsClient : TLS " + ex.Message);
}
Helper.WriteLog("sslStream after");
MemoryStream memoryStream = new MemoryStream();
Helper.WriteLog("MemoryStream");
BinaryWriter writer = new BinaryWriter(memoryStream);
Helper.WriteLog("BinaryWriter");
writer.Write((byte)0);
writer.Write((byte)0);
writer.Write((byte)32);
Helper.WriteLog("BinaryWriter Write");
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);
Helper.WriteLog("Load Play sound"); ;
writer.Write(b1);
writer.Flush();
byte[] array = memoryStream.ToArray();
Helper.WriteLog("Assign Memory stream"); ;
sslStream.Write(array);
Helper.WriteLog(array.Length.ToString());
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();
}
}