Iphone推送通知问题

时间:2010-11-05 05:45:28

标签: iphone notifications push

我正在开发iphone推送通知。我能够获得我的设备令牌。我正在使用.net来推送json数据。但问题是我无法收到通知。我只想知道.net是否必须在托管机器上推送json数据?

此致 Sathish所在

2 个答案:

答案 0 :(得分:1)

我认为这与:

有关

Add iPhone push notification using ASP.NET server

通读那里的链接项目。它非常全面。你实际上可以只使用那个库...它非常好。

除非我误解了这个问题?

答案 1 :(得分:0)

使用以下代码段发送推送通知

 public void PendingNotification(string DeviceToken,string message)
    {
        try
        {
            int port = 2195;
            //Developer
            String hostname = "gateway.sandbox.push.apple.com";
            //Production
            //String hostname = "gateway.push.apple.com";
            String certificatePassword = "XXXXXXX";
            string certificatePath = Server.MapPath("~/cer.p12");
            TcpClient client = new TcpClient(hostname, port);
            X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), certificatePassword);
            X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);
            SslStream sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);
            sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls, false);
            //String DeviceToken = "XXXXXXXXXXXX";
            String LoginName = "Name";
            int Counter = 1; //Badge Count;  
            String Message = message;
            String UID = "your choice UID";
            string payload = "{\"aps\":{\"alert\":\"" + Message + "\",\"badge\":" + Counter + ",\"sound\":\"default\"},\"UID\":\"" + UID + "\",\"LoginName\":\"" + LoginName + "\"}";
            MemoryStream memoryStream = new MemoryStream();
            BinaryWriter writer = new BinaryWriter(memoryStream);
            writer.Write((byte)0);
            writer.Write((byte)0);
            writer.Write((byte)32);
            writer.Write(HexStringToByteArray(DeviceToken.ToUpper()));
            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);
        }
        catch (Exception ex)
        {
            //Response.Write(ex.Message);
        }
    }
    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();
    }
    public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
    {
        if (sslPolicyErrors == SslPolicyErrors.None)
            return true;
        Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
        return false;
    }