c# - 从 asp.net C# 向 Iphone 发送推送通知

标签 c# ios asp.net iphone push-notification

` public void pushMessage(string deviceID)
        {
            int port = 30;
            String hostname = "gateway.sandbox.push.apple.com";
            String certificatePath = HttpContext.Current.Server.MapPath("PushKey.p12");
            X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), "mypassword");
            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, SslProtocols.Ssl3, false);
                MemoryStream memoryStream = new MemoryStream();
                BinaryWriter writer = new BinaryWriter(memoryStream);
                writer.Write((byte)0);
                writer.Write((byte)0);
                writer.Write((byte)32);

                writer.Write(HexStringToByteArray(deviceID.ToUpper()));
                String payload = "{\"aps\":{\"alert\":\"" + "Hi,, This Is a Sample Push Notification For IPhone.." + "\",\"badge\":1,\"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)
            {
                client.Close();
            }
            catch (Exception e)
            {
                client.Close();
            }
        }

              public static byte[] HexStringToByteArray(string hex)
        {
            return Enumerable.Range(0, hex.Length)
                             .Where(x => x % 2 == 0)
                             .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                             .ToArray();
        }`

错误如下所示:

System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 17.110.227.100:30

at System.Net.Sockets.TcpClient..ctor(String hostname, Int32 port)

最佳答案

端口应该是2195

https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/CommunicatingWIthAPS.html

同时将 SslProtocols.Ssl3 更改为 SslProtocols.Tls

    public static void pushMessage(string deviceID)
    {
        int port = 2195;
        String hostname = "gateway.push.apple.com";
        String certificatePath = System.Web.Hosting.HostingEnvironment.MapPath("pushkey.p12");
        X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), "yourPassword");
        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, SslProtocols.Tls, false);
            MemoryStream memoryStream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(memoryStream);
            writer.Write((byte)0);
            writer.Write((byte)0);
            writer.Write((byte)32);

            writer.Write(HexStringToByteArray(deviceID.ToUpper()));
            String payload = "{\"aps\":{\"alert\":\"" + "Hi,, This Is a Sample Push Notification For IPhone.." + "\",\"badge\":1,\"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)
        {
            client.Close();
        }
        catch (Exception e)
        {
            client.Close();
        }
    }

关于c# - 从 asp.net C# 向 Iphone 发送推送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28086819/

相关文章:

ios - 使用 Kingfisher ios 在同一网址中显示更新的图像

c# - 我无法在 ASP.NET 后面的代码中读取按下按钮的 css ID

asp.net - 查找 JS 错误 - "Stop running this script?"

c# - 从 C# 在 SQL Server 中批量更新

c# - 尝试从 DataGridView 计算 xml 值

iphone - Insert 语句不检查重复值

ios - Swift 中的 NSNotificationCenter addObserver

c# - asp.net 以编程方式创建表和 css 优先级

c# - 使用 .NET 4.0 编译器编译 .NET 3.5 项目

c# - 检查两个列表的修改