C# iPhone 推送服务器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1056083/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:08:40  来源:igfitidea点击:

C# iPhone push server?

c#iphonepush-notification

提问by Kyle

Im attempting to write a push server for the iPhone in C#. I have the following code:

我试图用 C# 为 iPhone 编写一个推送服务器。我有以下代码:

        // Create a TCP/IP client socket.
        using (TcpClient client = new TcpClient())
        {
            client.Connect("gateway.sandbox.push.apple.com", 2195);
            using (NetworkStream networkStream = client.GetStream())
            {
                Console.WriteLine("Client connected.");

                X509Certificate clientCertificate = new X509Certificate(@"certfile.p12", passwordHere);
                X509CertificateCollection clientCertificateCollection = new X509CertificateCollection(new X509Certificate[1] { clientCertificate });

                // Create an SSL stream that will close the client's stream.
                SslStream sslStream = new SslStream(
                    client.GetStream(),
                    false,
                    new RemoteCertificateValidationCallback(ValidateServerCertificate),
                    null
                    );

                try
                {
                    sslStream.AuthenticateAsClient("gateway.sandbox.push.apple.com");
                }
                catch (AuthenticationException e)
                {
                    Console.WriteLine("Exception: {0}", e.Message);
                    if (e.InnerException != null)
                    {
                        Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
                    }
                    Console.WriteLine("Authentication failed - closing the connection.");
                    client.Close();
                    return;
                }
            }

ect....

等等....

Only I keep receiving a exception: "A call to SSPI failed, see Inner exception" Inner Exception -> "The message received was unexpected or badly formatted."

只有我一直收到异常:“对 SSPI 的调用失败,请参阅内部异常”内部异常 ->“收到的消息意外或格式错误。”

Does anyone have any idea whats going wrong here?

有谁知道这里出了什么问题?

采纳答案by Kyle

Figured it out. Replaced sslStream.AuthenticateAsClient("gateway.sandbox.push.apple.com"); with sslStream.AuthenticateAsClient("gateway.sandbox.push.apple.com", clientCertificateCollection, SslProtocols.Default, false); And registered the certificates on the PC.

弄清楚了。替换了 sslStream.AuthenticateAsClient("gateway.sandbox.push.apple.com"); with sslStream.AuthenticateAsClient("gateway.sandbox.push.apple.com", clientCertificateCollection, SslProtocols.Default, false); 并在PC上注册了证书。

Edit: Here is the code for creating a payload as requested:

编辑:这是根据要求创建有效负载的代码:

    private static byte[] GeneratePayload(byte [] deviceToken, string message, string sound)
    {
        MemoryStream memoryStream = new MemoryStream();

        // Command
        memoryStream.WriteByte(0);

        byte[] tokenLength = BitConverter.GetBytes((Int16)32);
        Array.Reverse(tokenLength);
        // device token length
        memoryStream.Write(tokenLength, 0, 2);

        // Token
        memoryStream.Write(deviceToken, 0, 32);

        // String length
        string apnMessage = string.Format ( "{{\"aps\":{{\"alert\":{{\"body\":\"{0}\",\"action-loc-key\":null}},\"sound\":\"{1}\"}}}}",
            message,
            sound);

        byte [] apnMessageLength = BitConverter.GetBytes((Int16)apnMessage.Length);
        Array.Reverse ( apnMessageLength );
        // message length
        memoryStream.Write(apnMessageLength, 0, 2);

        // Write the message
        memoryStream.Write(System.Text.ASCIIEncoding.ASCII.GetBytes(apnMessage), 0, apnMessage.Length);

        return memoryStream.ToArray();
    } // End of GeneratePayload

回答by shader

Other way is just to use X509Certificate2 and X509CertificateCollection2 classes.

另一种方法是使用 X509Certificate2 和 X509CertificateCollection2 类。

回答by tobsen

I recently used Growl For Windowsto push messages to the Prowl client on the IPhone from .Net code. So you might get your functionatlity without writing a push server yourself.

我最近使用Growl For Windows将消息从 .Net 代码推送到iPhone 上的Prowl 客户端。因此,您无需自己编写推送服务器即可获得您的功能。

回答by tobsen

The "The message received was unexpected or badly formatted." error usually comes when you did not register the p12 certificate in Windows. (Under Vista, just double click on the p12 file and the import wizard will open)

“收到的消息意外或格式错误。” 当您没有在 Windows 中注册 p12 证书时,通常会出现错误。(在Vista下,只需双击p12文件,就会打开导入向导)

回答by Michael Donohue

From Zenox's comment: use a different version of AuthenticateAsClient

来自 Zenox 的评论:使用不同版本的 AuthenticateAsClient

sslStream.AuthenticateAsClient("gateway.sandbox.push.apple.com", clientCertificateCollection, SslProtocols.Default, false);

回答by muhammad kashif

In my case I had to delete all the certificate from my windows 8 and then re-install them in order to send push notifications to apple device.

就我而言,我必须从 Windows 8 中删除所有证书,然后重新安装它们,以便向 Apple 设备发送推送通知。

I do not know why my certificates stop working, I am searching for the correct reason and will update here soon.

我不知道为什么我的证书停止工作,我正在寻找正确的原因,很快就会在这里更新。