【问题标题】:C# iPhone push server?C# iPhone 推送服务器?
【发布时间】:2010-11-06 13:33:51
【问题描述】:

我正在尝试用 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;
                }
            }

等等……

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

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

【问题讨论】:

  • 想通了。替换 sslStream.AuthenticateAsClient("gateway.sandbox.push.apple.com");使用 sslStream.AuthenticateAsClient("gateway.sandbox.push.apple.com", clientCertificateCollection, SslProtocols.Default, false);并在PC上注册证书。
  • 是的,我一切正常,可以将消息推送到我的 iPhone。不要忘记在 windows 上注册你的 .p12 文件。
  • 你在 iPhone 上运行单声道吗?
  • 你应该回答你自己的问题来关闭它。
  • @Zenox - 注册 .p12 文件是必要的步骤吗?我想既然你是从文件中加载它,那没关系。

标签: c# iphone push-notification


【解决方案1】:

想通了。替换 sslStream.AuthenticateAsClient("gateway.sandbox.push.apple.com");使用 sslStream.AuthenticateAsClient("gateway.sandbox.push.apple.com", clientCertificateCollection, SslProtocols.Default, false);并在PC上注册证书。

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

    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

【讨论】:

  • 在 Windows Server 上我应该使用哪个文件夹(证书存储)来导入?它是个人还是受信任的发布者?我尝试了“基于证书类型的默认选择”但没有用。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    其他方法就是使用 X509Certificate2 和 X509CertificateCollection2 类。

    【讨论】:

      【解决方案4】:

      我最近使用Growl For Windows 向Prowl 客户端on the IPhone from .Net code 推送消息。因此,您无需自己编写推送服务器即可获得您的功能。

      【讨论】:

      • 这可能适用于将数据推送到 apn,但是如何处理反馈呢?
      【解决方案5】:

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

      【讨论】:

        【解决方案6】:

        在我的情况下,我必须从我的 Windows 8 中删除所有证书,然后重新安装它们才能将推送通知发送到苹果设备。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-08-06
          • 1970-01-01
          • 2011-03-09
          • 1970-01-01
          • 2010-11-27
          • 2012-01-21
          相关资源
          最近更新 更多