推送消息不会使用分布式证书发送

时间:2014-04-03 14:15:01

标签: c# iphone xcode apple-push-notifications

我可以使用沙箱发送推送器p12文件的推送通知,但我在appstore上的应用程序,我使用配置证书f12文件使用gateway.push.apple.com消息无法到达设备。

推送通知服务已在distirbuted证书上激活,我在developer.apple.com上查看,我也在尝试使用新的设备ID。 (不是开发者设备ID)

我如何解决我的问题?你可以帮帮我吗?我哪里错了?

我的C#代码如下:

int port = 2195;

    String hostname = "gateway.push.apple.com";

    String certificatePath = HttpContext.Current.Server.MapPath("distribution.p12");


    X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), "xxx", X509KeyStorageFlags.MachineKeySet |
    X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable);

    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, true);
    }
    catch (Exception e)
    {
        throw (e);
        client.Close();
        return;
    }
    MemoryStream memoryStream = new MemoryStream();
    BinaryWriter writer = new BinaryWriter(memoryStream);
    writer.Write((byte)0); 
    writer.Write((byte)0);  //The first byte of the deviceId length (big-endian first byte)
    writer.Write((byte)32); //The deviceId length (big-endian second byte)
    //String deviceID = "f1430c99 910d292d 2f756294 f2f6b348 153bc215 d5404447 16b294eb fdb9496c";
    writer.Write(HexStringToByteArray(deviceID.ToUpper()));
    String payload = "{\"aps\":{\"alert\":\"" + Mesaj + "\",\"badge\":0,\"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();

0 个答案:

没有答案