I have used following code to send push notifications to IOS devices using c#.
int port = 2195;
String hostname = "gateway.sandbox.push.apple.com";
String certificatePath = System.Web.Hosting.HostingEnvironment.MapPath("~/Resources/PushChatKey.p12");
X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), "p@ssw0rd");
X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);
TcpClient client = new TcpClient(hostname, port);
SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
try
{
sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Default, false);
MemoryStream memoryStream = new MemoryStream();
BinaryWriter writer = new BinaryWriter(memoryStream);
writer.Write((byte)0);
writer.Write((byte)0);
writer.Write((byte)32);
writer.Write(HexStringToByteArray(deviceID.ToUpper()));
String payload = "{\"aps\":{\"alert\":\"" + "Hi,, This Is a Sample Push Notification For IPhone.." + "\",\"badge\":1,\"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) { client.Close();
}
catch (Exception e) { client.Close();
}
I am getting Authentication failed because the remote party has closed the transport stream atsslStream.AuthenticateAsClient.
I know that many developers had faced the same situation. I tried all the solutions they suggested but nothing solved my issue.
I tried to use PushSharp as well, but i was getting the same error.
Please Help!