Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
public void PushNotificationToApple(string deviceID)
        {
            int port = 2195;
            String hostname = "gateway.sandbox.push.apple.com";

            //load certificate
            string certificatePath = HttpContext.Current.Server.MapPath("MPEDA_Production_Certificates.pem");          
            string certificatePassword = "xxxxx";
            X509Certificate2 clientCertificate = new X509Certificate2(certificatePath, certificatePassword);
            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.Tls, true);//Exception Line

            }
            catch (AuthenticationException ex)
            {
                client.Close();
                return;
            }

            // Encode a test message into a byte array.
            MemoryStream memoryStream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(memoryStream);

            writer.Write((byte)0);  //The command
            writer.Write((byte)0);  //The first byte of the deviceId length (big-endian first byte)
            writer.Write((byte)32); //The deviceId length (big-endian second byte)

            String deviceId = "DEVICEIDGOESHERE";
            writer.Write(ToByteArray(deviceId.ToUpper()));

            String payload = "{\"aps\":{\"alert\":\"I like spoons also\",\"badge\":14}}";

            writer.Write((byte)0); //First byte of payload length; (big-endian first byte)
            writer.Write((byte)payload.Length);     //payload length (big-endian second byte)

            byte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload);
            writer.Write(b1);
            writer.Flush();

            byte[] array = memoryStream.ToArray();
            sslStream.Write(array);
            sslStream.Flush();

            // Close the client connection.
            client.Close();
        }


        public static byte[] ToByteArray(string hex)
        {
            return Enumerable.Range(0, hex.Length)
                             .Where(x => x % 2 == 0)
                             .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                             .ToArray();
        }

        public static bool ValidateServerCertificate(
        object sender,
        X509Certificate certificate,
        X509Chain chain,
        SslPolicyErrors sslPolicyErrors)
        {
            if (sslPolicyErrors == SslPolicyErrors.None)
                return true;

            Console.WriteLine("Certificate error: {0}", sslPolicyErrors);

            // Do not allow this client to communicate with unauthenticated servers.
            return false;
        }


What I have tried:

Hello
I am Trying to send Push Notification on IOS and Write a Code above Mention

but i Get
Exception sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls, true);


SSl is not on my server so Please Help me How to Solve This Issues
Posted
Updated 5-May-18 0:49am
v3
Comments
Richard MacCutchan 5-May-18 7:30am    
I think the message "see inner exception" is a clue.
Member 12183079 5-May-18 7:31am    
so how to fix it
Richard MacCutchan 5-May-18 7:46am    
You first need to find out what is broken; and no one here can guess what your exception details are.
Dave Kreskowiak 5-May-18 8:09am    
When you get an Exception, it has a property called, oddly enough, "InnerException". Go look at that exception for the details.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900