来自c#的IPhone推送通知

时间:2012-12-26 14:43:47

标签: c# asp.net objective-c xcode

我们一直在使用Apple推送通知服务,我们可以从gateway.sandbox.push.apple.com发送推送消息 ,我只知道这个地址是测试的,所以我们必须使用真实地址gateway.push.apple.com,我们不能从这个地址发送推送消息。你能帮助我们吗?

我们的代码是;

public void pushMessage(string deviceID, string Mesaj)
{
    int port = 2195;
    String hostname = "gateway.sandbox.push.apple.com";     // TEST
    //String hostname = "gateway.push.apple.com";           // REAL

    //        @"cert.p12";
    String certificatePath = HttpContext.Current.Server.MapPath("cert.p12");
    //X509Certificate2 clientCertificate = new X509Certificate2(certificatePath, "");


    X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), "", 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);  //The command
    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)

    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();
}

您可以观看问题的视频

Video link

1 个答案:

答案 0 :(得分:2)

听起来你可能正在向Apple发送信息。此问题可能是您的iOS版本未使用生产推送通知服务器。

确保您的构建是Ad Hoc或Distribution,而不是开发。

Creating and Downloading a Distribution Provisioning Profile