0

We created the certificate .p12 using the Private Key only option. We tried push notification through MAC and its working fine. I used the below code which is not giving any error but iPhone is not getting any notification. In this Helper is my class to write log to file.

public void PushNotificationIOS(string message, string registrationKey, int type)
    {
        string deviceID = registrationKey; 
        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.Hosting.HostingEnvironment.MapPath(certPath);
        string certPassword = System.Configuration.ConfigurationManager.AppSettings["CertificatePassword"];
        TcpClient client = new TcpClient(hostname, port);
        try
        {
            X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), certPassword);
            X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);
            SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);

            try
            {
                sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls, false);
            }
            catch (Exception ex)
            {
                Helper.WriteLog("sslStream.AuthenticateAsClient : TLS  " + ex.Message);
            }

            MemoryStream memoryStream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(memoryStream);
            writer.Write((byte)0);
            writer.Write((byte)0);
            writer.Write((byte)32);
            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);
            writer.Write(b1);
            writer.Flush();
            byte[] array = memoryStream.ToArray();
            sslStream.Write(array);
            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();
        }

    }

Can anyone tell us how to trace the issue?

Wadhawan Vishal
  • 788
  • 1
  • 9
  • 15

1 Answers1

0

I think the problem is in .p12 creation process from KeyChain.

You can select the cert, and open the arrow to also select the private key and export them together as a .p12 file from Keychain Access.

enter image description here

From that you can generate .pem file using below command in terminal

cd
cd Desktop
openssl pkcs12 -in pushcert.p12 -out pushcert.pem -nodes -clcerts

You can refer to this for details info

Community
  • 1
  • 1
Sailendra
  • 1,318
  • 13
  • 25