2017-01-11 201 views
0

我一個一個發送通知給用戶,它需要一段時間。 這是我的代碼如何在一個請求中向多個設備(iOS)發送推送通知?

var deviceios = db.Devices; 

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


     string certificatePath = System.Web.HttpContext.Current.Server.MapPath("mypath/cert.p12); 

     string certificatePassword = "password"; 



     foreach (var item in deviceios) 
     { 


      X509Certificate2 clientCertificate = new X509Certificate2(certificatePath, certificatePassword, X509KeyStorageFlags.MachineKeySet); 
      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, System.Security.Authentication.SslProtocols.Default, false); 
      } 
      catch (AuthenticationException ex) 
      { 
       client.Close(); 
       return; 
      } 

      //// Encode a test message into a byte array. 
      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) 
      string devicetocken = item.device1;// iphone device token 
      byte[] b0 = HexString2Bytes(devicetocken); 
      WriteMultiLineByteArray(b0); 

      writer.Write(b0); 
      String payload; 
      string strmsgbody = ""; 
      int totunreadmsg = 1; 
      strmsgbody = message; 
      payload = "{\"aps\":{\"alert\":\"" + strmsgbody + "\",\"badge\":" + totunreadmsg.ToString() + ",\"sound\":\"mailsent.wav\"},\"acme1\":\"bar\",\"acme2\":42}"; 
      writer.Write((byte)0); //First byte of payload length; (big-endian first byte) 
      byte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload); 
      writer.Write((byte)b1.Length); //payload length (big-endian second byte) 
      writer.Write(b1); 
      writer.Flush(); 
      byte[] array = memoryStream.ToArray(); 
      try 
      { 
       sslStream.Write(array); 
       sslStream.Flush(); 
      } 
      catch 
      { 



      } 
      client.Close(); 

發送到多個設備的通知我必須單獨發送到每個設備。我可以向設備列表發送通知嗎?

+0

嘗試使用自己的服務器。在下一次更新中,您將放入一個偵聽Web服務器的函數,並在它觸發某個事件時指出推送事件。 – devRicher

+0

你有沒有考慮過使用ASP.Net SignalR?這是爲了這個目的。 –

+0

使用自己的服務器不會改變APN的工作方式。另外使用SignalR與推送通知完全不同。至於這個問題,你是不是打開和關閉每個通知的連接?這將需要很長時間。通知必須逐個發送,但可以在單個連接中發送。你只需要處理錯誤消息。 –

回答

0

由於您使用.NET發送通知,因此您可以嘗試使用Azure通知中心。有一個發送模板通知的選項,它允許您發送跨平臺通知。

https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-aspnet-cross-platform-notification

此外,您還可以指定標籤表達式,它允許你發送通知到與目標標記註冊的設備的特定羣體。我相信這可以幫助您通過一個請求向多個設備發送通知。結合使用和使用模板,您甚至可以通過單個請求將通知發送到多個Android,Windows Phone和iOS設備。

https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-tags-segment-push-message

你可以找到更多關於這裏Azure的通知中心:

https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-push-notification-overview

相關問題