Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to send the Push notification to send ios device using WCF service.
When i run the service from Local Machine the push notification is send to the server
but when it is hosted on iis notification is not send.

i am receiving following error:

error:-
A call to SSPI failed, see inner exception


inner exception:
System.ComponentModel.Win32Exception " +
    "(0x80004005): The message received was unexpected or badly formatted


What I have tried:

public string PushNotificationIOS(string deviceID)
        {
            string devicetocken=string.Empty;//  iphone device token

            try
            {

                //devicetocken = "";//  iphone device token
                devicetocken = deviceID;//  iphone device token
                int port = 2195;
                String hostname = "gateway.sandbox.push.apple.com";
                //String hostname = "gateway.push.apple.com";

                string certificatePath = System.Web.Hosting.HostingEnvironment.MapPath("~/Certificate/Certificates.pem");
                //Server.MapPath("final.p12");

                string certificatePassword = "Sudesi@123";// "mohs123$";

                X509Certificate2 clientCertificate = new X509Certificate2(certificatePath, certificatePassword, X509KeyStorageFlags.MachineKeySet);
                X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);

                TcpClient client = new TcpClient(hostname, port);
                SslStream sslStream = new SslStream(
                                client.GetStream(),
                                false,
                                new RemoteCertificateValidationCallback(ValidateServerCertificate),
                                null
                );


                sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls, false);



                //// 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)

                byte[] b0 = HexString2Bytes(devicetocken);
               // WriteMultiLineByteArray(b0);

                writer.Write(b0);
                String payload;
                string strmsgbody = "";
                int totunreadmsg = 20;
                strmsgbody = "Your call for complaint no. M00011 has been registered in our system. - Your ticket no for your communication is 100000000065";

                //Debug.WriteLine("during testing via device!");
                //Request.SaveAs(Server.MapPath("APNSduringdevice.txt"), true);

                payload = "{\"aps\":{\"alert\":\"" + strmsgbody + "\",\"badge\":" + totunreadmsg.ToString() + ",\"sound\":\"mailsent.wav\"},\"acme1\":\"bar\",\"acme2\":42}";

                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();
                //Debug.WriteLine("This is being sent...\n\n");
                //Debug.WriteLine(array);

                sslStream.Write(array);
                sslStream.Flush();


                //Debug.WriteLine("Write failed buddy!!");
                //Request.SaveAs(Server.MapPath("Writefailed.txt"), true);


                client.Close();
                //Debug.WriteLine("Client closed.");
                //Request.SaveAs(Server.MapPath("APNSSuccess.txt"), true);

                return "True";
            }
            catch (AuthenticationException ex)
            {
                //Console.WriteLine("Authentication failed");
                //client.Close();
                //Request.SaveAs(Server.MapPath("Authenticationfailed.txt"), true);
                return ex.Message;
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }
Posted

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