Guest User

Untitled

a guest
May 5th, 2016
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. public void iphonpushnotification(string deviceid)
  2. {
  3. string devicetocken =deviceid;// iphone device token
  4. int port = 2195;
  5. String hostname = "gateway.sandbox.push.apple.com";
  6. //String hostname = "gateway.push.apple.com";
  7.  
  8. // string certificatePath = Server.MapPath("E:\Certificates.p12");
  9. string certificatePath = "E:\pushNotification.p12";
  10.  
  11. string certificatePassword = "abc123";
  12.  
  13. X509Certificate2 clientCertificate = new X509Certificate2(certificatePath, certificatePassword, X509KeyStorageFlags.MachineKeySet);
  14. X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(clientCertificate);
  15.  
  16. TcpClient client = new TcpClient(hostname, port);
  17. SslStream sslStream = new SslStream(
  18. client.GetStream(),
  19. false,
  20. new RemoteCertificateValidationCallback(ValidateServerCertificate),
  21. null
  22. );
  23.  
  24. try
  25. {
  26. sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Default, false);
  27. }
  28. catch (AuthenticationException ex)
  29. {
  30. Console.WriteLine("Authentication failed");
  31. client.Close();
  32. // Request.SaveAs(Server.MapPath("Authenticationfailed.txt"), true);
  33. return;
  34. }
  35.  
  36.  
  37. //// Encode a test message into a byte array.
  38. MemoryStream memoryStream = new MemoryStream();
  39. BinaryWriter writer = new BinaryWriter(memoryStream);
  40.  
  41. writer.Write((byte)0); //The command
  42. writer.Write((byte)0); //The first byte of the deviceId length (big-endian first byte)
  43. writer.Write((byte)32); //The deviceId length (big-endian second byte)
  44.  
  45. byte[] b0 = HexString2Bytes(devicetocken);
  46. WriteMultiLineByteArray(b0);
  47.  
  48. writer.Write(b0);
  49. String payload;
  50. string strmsgbody = "";
  51. int totunreadmsg = 20;
  52. strmsgbody = "Hey Aashish!";
  53.  
  54. Debug.WriteLine("during testing via device!");
  55. // Request.SaveAs(Server.MapPath("APNSduringdevice.txt"), true);
  56.  
  57. payload = "{"aps":{"alert":"" + strmsgbody + "","badge":" + totunreadmsg.ToString() + ","sound":"mailsent.wav"},"acme1":"bar","acme2":42}";
  58.  
  59. writer.Write((byte)0); //First byte of payload length; (big-endian first byte)
  60. writer.Write((byte)payload.Length); //payload length (big-endian second byte)
  61.  
  62. byte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload);
  63. writer.Write(b1);
  64. writer.Flush();
  65.  
  66. byte[] array = memoryStream.ToArray();
  67. Debug.WriteLine("This is being sent...nn");
  68. Debug.WriteLine(array);
  69. try
  70. {
  71. sslStream.Write(array);
  72. sslStream.Flush();
  73. }
  74. catch
  75. {
  76. Debug.WriteLine("Write failed buddy!!");
  77. // Request.SaveAs(Server.MapPath("Writefailed.txt"), true);
  78. }
  79.  
  80. client.Close();
  81. Debug.WriteLine("Client closed.");
  82. // Request.SaveAs(Server.MapPath("APNSSuccess.txt"), true);
  83. }
  84. private byte[] HexString2Bytes(string hexString)
  85. {
  86. //check for null
  87. if (hexString == null) return null;
  88. //get length
  89. int len = hexString.Length;
  90. if (len % 2 == 1) return null;
  91. int len_half = len / 2;
  92. //create a byte array
  93. byte[] bs = new byte[len_half];
  94. try
  95. {
  96. //convert the hexstring to bytes
  97. for (int i = 0; i != len_half; i++)
  98. {
  99. bs[i] = (byte)Int32.Parse(hexString.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber);
  100. }
  101. }
  102. catch (Exception ex)
  103. {
  104. //MessageBox.Show("Exception : " + ex.Message);
  105. }
  106. //return the byte array
  107. return bs;
  108. }
  109. // The following method is invoked by the RemoteCertificateValidationDelegate.
  110. public static bool ValidateServerCertificate(
  111. object sender,
  112. X509Certificate certificate,
  113. X509Chain chain,
  114. SslPolicyErrors sslPolicyErrors)
  115. {
  116. if (sslPolicyErrors == SslPolicyErrors.None)
  117. return true;
  118.  
  119. Console.WriteLine("Certificate error: {0}", sslPolicyErrors);
  120.  
  121. // Do not allow this client to communicate with unauthenticated servers.
  122. return false;
  123. }
  124. public static void WriteMultiLineByteArray(byte[] bytes)
  125. {
  126. const int rowSize = 20;
  127. int iter;
  128.  
  129. Console.WriteLine("initial byte array");
  130. Console.WriteLine("------------------");
  131.  
  132. for (iter = 0; iter < bytes.Length - rowSize; iter += rowSize)
  133. {
  134. Console.Write(
  135. BitConverter.ToString(bytes, iter, rowSize));
  136. Console.WriteLine("-");
  137. }
  138.  
  139. Console.WriteLine(BitConverter.ToString(bytes, iter));
  140. Console.WriteLine();
  141. }
Add Comment
Please, Sign In to add comment