2013-03-08 16 views
4
using System; 
using System.Collections.Generic; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Net; 
using System.IO; 
using System.Text; 
using System.Security.Authentication; 
using System.Net.Security; 
using System.Net.Sockets; 
using System.Security.Cryptography.X509Certificates; 
using Newtonsoft.Json.Linq; 

namespace WebApplication1 
{ 
    public partial class _Default : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 

     } 

     protected void btnPush_Click(object sender, EventArgs e) 
     { 
      pushMessage(txtDeviceID.Text.Trim(), txtPayload.Text.Trim()); 
     } 


     public void pushMessage(string deviceID, string Mesaj) 
     { 

      int port = 2195; 
      String hostname = "gateway.push.apple.com"; 

      String certificatePath = HttpContext.Current.Server.MapPath("new_dev_cert.p12"); 
      X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), "taxmann"); 
      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); //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\":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(); 
       lblResponse.Text = "Sucess.."; 
      } 
      catch (System.Security.Authentication.AuthenticationException ex) 
      { 
       client.Close(); 
       lblResponse.Text = ex.Message; 
      } 
      catch (Exception e) 
      { 
       client.Close(); 
       lblResponse.Text = e.Message; 
      } 
     } 

     // The following method is invoked by the RemoteCertificateValidationDelegate. 
     public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
     { 
      if (sslPolicyErrors == SslPolicyErrors.None) 
       return true; 
      else // Do not allow this client to communicate with unauthenticated servers. 
       return false; 
     } 

     private static byte[] HexStringToByteArray(String DeviceID) 
     { 
      //convert Devide token to HEX value. 
      byte[] deviceToken = new byte[DeviceID.Length/2]; 
      for (int i = 0; i < deviceToken.Length; i++) 
       deviceToken[i] = byte.Parse(DeviceID.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber); 

      return deviceToken; 
     } 
    } 
} 

以上是我发送通知后端的源代码。我的代码调试成功并获取消息。但我无法在iPhone上获取通知。如何准备后端使用C#.net推送iPhone中的通知

我跟随此http://blog.serverdensity.com/how-to-build-an-apple-push-notification-provider-server-tutorial/。我不知道问题在哪里。你能帮我解决这个问题吗?

+0

只是为了确保您添加推送通知的应用程序签名证书? – shoughton123 2013-03-08 10:49:30

+0

我已经添加了,并且代码能够读取该证书...代码能够读取所有行,当我调试得到回应 – user2148026 2013-03-08 10:52:21

+0

我已经尝试了很多,但不能做请帮助我 – user2148026 2013-03-08 10:53:08

回答

0

通过本教程,它解释了从开始到结束所需的一切,并且完美地工作。它解释了如何设置服务器端和iOS设备上的所有内容,以及如何在开发人员门户中整理所有证书。

http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12

希望这有助于

山姆

{ 
    "aps": 
    { 
     "alert": 
     { 
      "body": "Hello, world!" 
     }, 
     "badge": 2 
    } 
} 
+0

@sam可以ü请检查数据,即时通讯发送:字符串有效负载=“{\”aps \“:{\ “alert \”:\“”+ Mesaj +“\”,\“badge \”:1,\“sound \”:\“default \”}}“;是正确的JSOn格式? – user2148026 2013-03-08 11:16:30

+0

@ shoughton123请告诉我亲爱的? – user2148026 2013-03-08 11:26:54

+0

对我来说看起来很好。让我只是在记事本中检查它很难阅读:) – shoughton123 2013-03-08 11:28:47