2

我已經安裝併成功測試APNS通訊與開發證書和gateway.sandbox.push.apple.com
當我切換到我的分發證書和gateway.push.apple.com消息沒有通過,我收到一個異常:無法發送APNS消息的發佈證書

無法將數據寫入傳輸連接:建立的連接被主機中的軟件中止。

我按照這些教程來生成P12證書:

http://www.raywenderlich.com/32960/apple-push-notification-services-in-ios-6-tutorial-part-1 http://code.google.com/p/apns-sharp/wiki/HowToCreatePKCS12Certificate

服務器軟件我從發送通知是使用SslStreamX509Certificate2類.NET應用程序。

下面是連接代碼:

/// <summary> 
    /// Establish connection to Apple's Push Notification System using the variables in the config file. 
    /// </summary> 
    public void ConnectToAPNS() 
    { 
     try 
     { 
      int port = Convert.ToInt32(Config.SSL_PORT); // 2195 
       // pick the target address 
      String hostname = Config.SSL_PATH; // gateway.push.apple.com 

      //load certificate 
      string certificatePath = Config.CERT_FILE; //.p12 certification file 
      string certificatePassword = Config.CERT_PASS; 
      byte[] p12Data = System.IO.File.ReadAllBytes(certificatePath); 

      X509Certificate2 clientCertificate = new X509Certificate2(p12Data, certificatePassword, X509KeyStorageFlags.MachineKeySet | X509KeyStorageFlags.PersistKeySet | X509KeyStorageFlags.Exportable); 

      X509Certificate2Collection certificatesCollection = new X509Certificate2Collection(); 
      certificatesCollection.Add(clientCertificate); 

      RemoteCertificateValidationCallback remote = new RemoteCertificateValidationCallback(ValidateServerCertificate); 

      client = new TcpClient(hostname, port); 
      sslStream = new SslStream(
        client.GetStream(), 
        false, 
        remote, 
        null 
      ); 

      try 
      { 
       sslStream.AuthenticateAsClient(hostname, certificatesCollection, SslProtocols.Tls, true); 

      } 
      catch (AuthenticationException e) 
      { 
       IQLogger.Logger.LogError("Push NotifyMessenger ConnectToAPNS: " + e.Message); 
       client.Close(); 
       sslStream = null; 
       client = null; 
       return; 
      } 
     }catch (Exception e) 
     { 
      IQLogger.Logger.LogError("PushNotifyMessenger ConnectToAPNS: " + e.Message); 
      sslStream = null; 
      client = null; 
      return; 
     } 
     return; 
    } 

    /// <summary> 
    /// Call back checks for validation error 
    /// </summary> 
    /// <returns></returns> 
    public static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors) 
    { 
     try{ 
      if (sslPolicyErrors == SslPolicyErrors.None) 
       return true; 
     } 
     catch 
     { 
     } 
     IQLogger.Logger.LogError("PushNotifyMessenger ValidateServerCertificate: sslPolicyErrors" + sslPolicyErrors); 
     // Do not allow this client to communicate with unauthenticated servers. 
     return false; 
    } 

這是發送消息的代碼:

/// <summary> 
    /// I send a text message to a specified device ID. I provide an example of how to format a message for the APNS system. 
    /// The message must be formatted in Json and be sent as a byte array. 
    /// </summary> 
    /// <param name="deviceId"> A string containing 64 characters representing hexidecimal values of a 32 byte device code</param> 
    /// <param name="msgPrompt">The text that will popup on the device </param> 
    /// <param name="msgData"> XML that will be put in the XML json Section. This data will be invisible to the recipient's user, 
    /// but will be accessible by the recipient program</param> 
    public void SendMessage(string deviceId, string msgPrompt, string msgData) 
    { 
     try{ 
      if (client == null || sslStream == null) 
      { 
       ConnectToAPNS(); 
      } 

      // 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) 

      byte[] arDeviceID = ConvertDeviceID(deviceId);//System.Text.Encoding.UTF8.GetBytes(deviceId); 
      writer.Write(arDeviceID); 

      String payload = "{\"aps\":{\"alert\":\"" + msgPrompt + "\",\"badge\":1,\"xml\":\"" + msgData + "\"}}"; 

      writer.Write((byte)0); //First byte of payload length; (big-endian first byte) 
      writer.Write((byte)payload.Length); //payload length (big-endian second byte) 

      byte[] b1 = System.Text.Encoding.UTF8.GetBytes(payload); 
      writer.Write(b1); 
      writer.Flush(); 

      byte[] array = memoryStream.ToArray(); 
      sslStream.Write(array); // <<<<<<<<<< exception thrown here 
      sslStream.Flush(); 

     } 
     catch (Exception e) 
     { 
      IQLogger.Logger.LogError("PushNotifyMessenger.SendMessage: " + e.Message); 
      return; 
     } 

    } 

拋出異常的第二或第三嘗試發送APNS消息。然而,沒有任何消息傳遞到設備。我可以交換回我的開發證書,並且此代碼功能正常(並且APNS消息到達設備)。

我在尋找解決我所遇到的帖子,指出開發證書以某種方式與部署一個干擾,但是我不明白這是怎麼發生的事情或者我應該怎麼做來解決它的問題。

在此先感謝您提供的任何幫助。

回答

0

確保您使用APNS Live服務器URL SSL://gateway.push.apple.com:2195