2

You can find similar code examples in many places on the internet:

var apnsHost = "gateway.sandbox.push.apple.com";
var apnsPort = 2195;
var timeout = 3000;

using(TcpClient client = new TcpClient(AddressFamily.InterNetwork))
{
    await client.ConnectAsync(apnsHost, apnsPort);

    using (SslStream sslStream = new SslStream(client.GetStream(), false, _certificateValidationCallback, null))
    {
        try
        {
            sslStream.AuthenticateAsClient(apnsHost, _certificateCollection, System.Security.Authentication.SslProtocols.Tls, true);
        } catch {
            throw;
        }

        MemoryStream memoryStream = new MemoryStream();
        BinaryWriter writer = new BinaryWriter(memoryStream);

        byte[] binaryToken = StringToByteArray(deviceToken);
        writer.Write((byte)0); // The command
        writer.Write((byte)0); // The first byte of the deviceId length (bit-endian first byte)
        writer.Write((byte)32); // The deviceId length (big-endian second byte)
        writer.Write(binaryToken);

        byte[] bytesPayload = Encoding.UTF8.GetBytes(payload);
        writer.Write((byte)0);
        writer.Write((byte)bytesPayload.Length);
        writer.Write(bytesPayload);
        writer.Flush();

        byte[] array = memoryStream.ToArray();
        sslStream.Write(array);
        sslStream.Flush();
    }
}

While I understand that this code shakes hands with APNS, establishes a TLS connection and writes appropriate request content, I don't really understand where in this code I can specify additional Headers!

Apple describes here various headers that can be specified, and I am interested in specifying apns-expiration and apns-priority but I just can't figure out where and what kind of code can I fit in here to achieve my goal?

Mikser
  • 929
  • 10
  • 16

1 Answers1

0

I think when you have the TCP Client connect (not connectAsync) you can specify an IPEndPoint object and use that do set the headers.

TCP Client Connect: https://learn.microsoft.com/en-us/dotnet/api/system.net.sockets.tcpclient.connect?view=netframework-4.7.2#System_Net_Sockets_TcpClient_Connect_System_Net_IPEndPoint_

IPEndPoint: https://learn.microsoft.com/en-us/dotnet/api/system.net.ipendpoint?view=netframework-4.7.2

I've never tried sending a post like that so not sure what would be the JSON for your request.

Sean
  • 1,359
  • 11
  • 15