How to get response from APNS using C #

I am creating one web api application and I want to send notification to iOS devices.

I tried with pushsharp but it only works fine with a few devices, sending it to multiple devices with expired / invalid tokens without sending notifications for all devices.

So, I proceed with the following code:

public async Task pushMessage(string deviceToken, string message)
{
  int port = 2195;
  String hostname = "gateway.sandbox.push.apple.com";

  String certificatePath = "~/test.p12" // my certificate path 

  X509Certificate2 clientCertificate = new X509Certificate2(System.IO.File.ReadAllBytes(certificatePath), CommonSource.GetAppleCertificatePassword(), 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, SslProtocols.Tls, false);
     MemoryStream memoryStream = new MemoryStream();
     BinaryWriter writer = new BinaryWriter(memoryStream);
     writer.Write((byte)0);
     writer.Write((byte)0);
     writer.Write((byte)32);

     writer.Write(HexStringToByteArray(deviceToken.ToUpper()));    
     String payload = message.ToString(); 

     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();

     // Read message from the server. 
     //byte[] response = ReadMessage(sslStream);   

     client.Close();
   }
  catch (System.Security.Authentication.AuthenticationException ex)
  {
     LogError(ex.Message);
     client.Close();
  }
  catch (Exception e)
  {
    LogError(e.Message);
    client.Close();
   }            
} 

      

This code works exactly as I wanted.
But now I want to check the response from the APNS server. So I checked with the byte[] response = ReadMessage(sslStream);

ReadMessage method looks like this:

private byte[] ReadMessage(SslStream sslStream)
{
    MemoryStream ms = new MemoryStream();

    byte[] buffer = new byte[1024];

    int bytes = -1;
    do
    {
        bytes = sslStream.Read(buffer, 0, buffer.Length);

        ms.Write(buffer, 0, bytes);

    } while (bytes != 0);

    return ms.ToArray();
}

      

But when I run this, it is stuck at bytes = sslStream.Read(buffer, 0, buffer.Length);

I cannot figure out the problem.

Can anyone tell me what is the problem with my code?

+3


source to share


1 answer


According to this article

Apples Notification Service Problem ... Solutions and Workarounds ...



  • Apple never responds for successful messages.

  • An error response cannot be returned immediately before you have a chance to send more notifications to the stream, but before Apple closes the connection. These notifications, which may still pass, remain uncertain. They are never acknowledged or transmitted, and no error responses are returned for them.

Therefore, in the event that your request works as intended, there will be no response. This will mean that your attempt to read the response will get nothing, and this is what you experience when you cannot get through the line you read. He is waiting for an answer that will never appear.

+5


source







All Articles