0

从 C# 代码向苹果手机发送推送通知时,我收到此错误。我搜索了这个问题,但这些答案无法解决这个问题。任何人都可以给我建议来解决这个问题。

       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 { }
        }
4

1 回答 1

0

您是否提供了证书密码来验证您的证书?

我建议使用PushSharp节省时间并摆脱混乱的编码。

PushSharp 是一个服务器端库,用于向 iOS(iPhone/iPad APNS)、Android(C2DM 和 GCM - Google Cloud Message)、Windows Phone、Windows 8、Amazon、Blackberry 和(即将推出的)FirefoxOS 设备发送推送通知!

于 2015-09-22T07:26:14.510 回答