首页 > 解决方案 > IOS推送通知未从c#收到

问题描述

我检查了这么多链接,我试过沙箱有主机名,也在本地计算机上安装了 .p12 文件。在这段代码完美运行之前。以及如何在发送推送通知时检查来自苹果的响应。代码按预期完美处理,但未收到推送通知。

try
{
    int port = 2195;
   
    String hostname = "gateway.push.apple.com";
    
    //String certificatePath = "";
    String certificatePath = HttpContext.Current.Server.MapPath("~/pushcert.pem");
    X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), "");
    X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);
    TcpClient client = new TcpClient(hostname, port);
    SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
    try
    {
        ServicePointManager.Expect100Continue = true;
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3 | SecurityProtocolType.Tls | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;
        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);

         



        var aps1 = new
        {
            alert = Subject,
            messageId = "test",
            type = type,
            badge = 1,
            sound = "default"
        };
        var aps = new
        {
            aps = aps1
        };



        var json = JsonConvert.SerializeObject(aps);

        writer.Write(HexStringToByteArray(deviceID.ToUpper()));
        String payload = json; 
        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)
    {
        saveException(ex.Message, "Failed", "MobileAPI/SendMessage");
        client.Close();
    }
    catch (Exception e)
    {
        saveException(e.Message, "Failed", "MobileAPI/SendMessage");
        client.Close();
    }
}
catch (Exception e)
{
    saveException(e.Message, "Failed", "MobileAPI/SendMessage");
}

标签: c#push-notificationapple-push-notifications

解决方案


推荐阅读