0

While sending push notifications from C# code to apple phone i am getting this error. I googled for this issue but those answers could not solve this problem. Can any one give me suggestions to fix this issue.

       protected void Page_Load(object sender, EventArgs e)
        {
            string deviceID = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
            string certificateFilePath ="XXXXXXX.cer";
            pushMessage(deviceID, certificateFilePath);
        }
        public void pushMessage(string deviceID, string certificateFilePath)
        {
            try
            {
                int port = 2195;
                String hostname = "gateway.sandbox.push.apple.com";
                String certificatePath = HttpContext.Current.Server.MapPath(certificateFilePath);
                X509Certificate2 clientCertificate = new X509Certificate2(certificatePath);
                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, System.Security.Authentication.SslProtocols.Default, true);
                //sslStream.AuthenticateAsClient(hostname, certificatesCollection, System.Security.Authentication.SslProtocols.Default, false);
                //sslStream.AuthenticateAsClient(hostname, certificatesCollection, System.Security.Authentication.SslProtocols.Tls, true);
                }
                catch (AuthenticationException ex)
                {
                    client.Close();
                    return;
                }
                MemoryStream memoryStream = new MemoryStream();
                BinaryWriter writer = new BinaryWriter(memoryStream);
                writer.Write((byte)0);  
                writer.Write((byte)0); 
                writer.Write((byte)32); 
                String deviceId = deviceID;
                writer.Write(HexStringToByteArray(deviceId.ToUpper()));
                String payload = "{\"aps\":{\"alert\":\"Test...\",\"badge\":1}}";
                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 { }
        }
Rao
  • 71
  • 1
  • 9

1 Answers1

0

Have you provide Certificate password to authenticate your certificate?

I recommended to use PushSharp save time and rids of messy coding.

PushSharp is a server-side library for sending Push Notifications to iOS (iPhone/iPad APNS), Android (C2DM and GCM - Google Cloud Message), Windows Phone, Windows 8, Amazon, Blackberry, and (soon) FirefoxOS devices!

Bhavik Patel
  • 1,466
  • 1
  • 12
  • 28