用asp.net问题推送ios推送通知?

时间:2016-02-04 11:48:23

标签: c# asp.net .net

我正在使用.net服务器为iOS设备提供推送通知。在我的服务器端口:2195& 2196已经打开。如果我在PHP端使用iOS .p12 / .pem文件(同一服务器) - >它工作正常。但在.net方面,我无法发送通知

这是我的代码

[HttpGet]
[ActionName("Checkpush")]

public Object PushNotificationToApple(string deviceid)
{
// var push = new PushBroker();
//Wire up the events for all the services that the broker registers
//push.OnNotificationSent += NotificationSent;
//push.OnChannelException += ChannelException;
//push.OnServiceException += ServiceException;
//push.OnNotificationFailed += NotificationFailed;
//push.OnDeviceSubscriptionExpired += DeviceSubscriptionExpired;
//push.OnDeviceSubscriptionChanged += DeviceSubscriptionChanged;
//push.OnChannelCreated += ChannelCreated;
//push.OnChannelDestroyed += ChannelDestroyed;

//try
//{
// var appleCert = File.ReadAllBytes(Path.Combine(AppDomain.CurrentDomain.BaseDirectory,"~/apns-dev-cert.p12"));
// push.RegisterAppleService(new ApplePushChannelSettings(false, appleCert, "welcome123"));
// push.QueueNotification(new AppleNotification()
//  .ForDeviceToken("cd2887c4093569b3eed142320f21a81e521e486cf5c40467390901d3a191398b")//the recipient device id
// .WithAlert("Hello")//the message
// .WithBadge(1)
// .WithSound("default")
// );
//}
//catch (Exception ex)
//{
// throw ex;
//}
//push.StopAllServices(waitForQueuesToFinish: true);


Int64 result = 0;
////// Messages message = null;
//try
//{
// string strDeviceToken = "cd2887c4093569b3eed142320f21a81e521e486cf5c40467390901d3a191398b";
// string strPushMessage = "Good Morning!";
// var payload1 = new NotificationPayload(strDeviceToken, strPushMessage, 1, "default");
// payload1.AddCustom("RegionID", "IDQ10150");
// var p = new List<NotificationPayload> { payload1 };
// string certificatePath = System.Web.Hosting.HostingEnvironment.MapPath("~/apns-dev-cert.p12");

// var push = new PushNotification(true, certificatePath, "welcome123");
// // push.P12File = "D:/Projects/RKB_RestAPI/apns-dev-cert.p12";
// string strfilename = push.P12File;
// var message1 = push.SendToApple(p);
// foreach (var item in message1)
// {
// result = 1;
// }
//}
//catch (Exception ex)
//{
//}
// return result;
// int result=0;
int port = 2195;
String hostname = "gateway.push.apple.com";


//String certificatePath = new     X509Certificate2(System.IO.File.ReadAllBytes(p12File)
 // , p12FilePassword
// , X509KeyStorageFlags.MachineKeySet |
// X509KeyStorageFlags.PersistKeySet |
// X509KeyStorageFlags.Exportable);
X509Certificate2 clientCertificate = new     X509Certificate2(System.IO.File.ReadAllBytes(System.Web.Hosting.HostingEnvironme nt.MapPath("~/apns-dev-cert.p12"))
    , "welcome123"
  , 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.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\":\"" +"text messages" +"\",\"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;
   }
   return result;
  }

我收到错误

{"Message":"An error has occurred.","ExceptionMessage":"Transaction failed. The server response was: 5.7.1 Forged HELO .","ExceptionType":"System.Net.Mail.SmtpException","StackTrace":"   at System.Net.Mail.MailCommand.CheckResponse(SmtpStatusCode statusCode, String response)\r\n   at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode)\r\n   at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)\r\n   at System.Net.Mail.SmtpClient.Send(MailMessage message)\r\n   at RKB.RestAPI.V1Controller.Forgot(String emailid) in d:\\Projects\\RKB_RestAPI\\V1.cs:line 391\r\n   at lambda_method(Closure , Object , Object[] )\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object instance, Object[] methodParameters)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.<>c__DisplayClass5.<ExecuteAsync>b__4()\r\n   at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)"}

我不知道从哪里弄错了。你能帮帮我吗

0 个答案:

没有答案