APNS抛出错误"对SSPI的调用失败,请参阅内部异常。 - 收到的消息是意外的或格式错误的#34;

时间:2017-05-23 07:49:47

标签: ssl apple-push-notifications

我收到此异常"对SSPI的调用失败,请参阅内部异常",内部异常"收到的消息是意外或格式错误"

尝试调用此方法时:

sslStream.AuthenticateAsClient(apsHost, certs, System.Security.Authentication.SslProtocols.Tls, false);

我使用生产证书并在2195端口调用生产APNS服务器(" gateway.push.apple.com")。

我知道有几个帖子(大多数都是旧的)但我真的尝试过我在其他帖子上找到的所有内容以及谷歌上的不同搜索,没有任何内容适用于我的问题。

我发现的大多数答案都是将SSL协议从Ssl3更改为Tls,所以我这样做了,它没有用。

还尝试使用xcode从apple重新创建推送服务证书,然后重新安装。

如果有人知道我错过了什么,我会感激你的帮助

这是代码的其余部分:

private void PushIosNotification(object notificationData)
    {
        Log.Get().Info(Log.BuildMessageEx(notificationData));
        SslStream sslStream = null;
        TcpClient tcpClient = null;
        MemoryStream ms = null;
        BinaryWriter bw = null;
        try
        {
            NotificationData nData = (NotificationData)notificationData;
            X509Certificate2Collection certs = new X509Certificate2Collection();
            if (certs != null)
            {
                certs.Add(getServerCert(nData.AppleProductionPushCertificateFriendlyName, nData.AppleDevelopmentPushCertificateFriendlyName, nData.IsProduction));

                string apsHost = certs[0].ToString().Contains("Production") ? nData.AppleServerURL : nData.AppleSandBoxServerURL;


                tcpClient = new TcpClient(apsHost, int.Parse(nData.AppleServerPort));

                sslStream = new SslStream(tcpClient.GetStream());

                sslStream.AuthenticateAsClient(apsHost, certs, System.Security.Authentication.SslProtocols.Tls, false);

                byte[] buf = new byte[256];

                ms = new MemoryStream();
                bw = new BinaryWriter(ms, Encoding.UTF8);

                bw.Write(new byte[] { 0, 0, 32 });
                bw.Write(hexToData(nData.DeviceId));
                bw.Write((byte)0);

                // Create the APNS payload - new.caf is an audio file saved in the application bundle on the device
                string msg = "{\"aps\":{\"alert\":\"" + nData.Message.Replace("₪", "₪").Replace("ש\"ח", "₪") + "\",\"badge\":" + nData.IBadge + ",\"sound\":\"new.caf\",\"url\":\"www.google.com\"}}";

                UTF8Encoding encoding = new UTF8Encoding();
                byte[] postBytes = encoding.GetBytes(msg);

                // Write the data out to the stream
                bw.Write((byte)postBytes.Length);
                bw.Write(postBytes);
                bw.Flush();

                sslStream.Write(ms.ToArray());
            }
        }
        catch (System.Security.Cryptography.CryptographicException CrEx)
        {
            Log.Get().Error("CryptographicException ConnectToAPNS1:", CrEx);
        }
        catch (Exception e)
        {
            Log.Get().Error("PushIosNotification", e);
        }
        finally
        {
            if (tcpClient != null)
            {
                tcpClient.Close();
            }
            if (sslStream != null)
            {
                sslStream.Close();
            }
            if (ms != null)
            {
                ms.Close();
            }
            if (bw != null)
            {
                bw.Close();
            }
        }
    }

谢谢,

1 个答案:

答案 0 :(得分:0)

好了,现在它正常工作,除了创建一个具有相同代码的控制台应用程序之外什么也没做,它在那里工作,然后原始代码也开始工作..