如何使用标准C#.NET使用“apns-expiration”标头发送推送通知?

问题描述 投票:1回答:1

您可以在互联网上的许多地方找到类似的代码示例:

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

虽然我知道这个代码与APNS握手,建立TLS连接并写入适当的请求内容,但我真的不明白在这段代码中我可以指定其他标题!

Apple描述了here可以指定的各种标题,我有兴趣指定apns-expirationapns-priority,但我无法弄清楚我可以在哪里和什么样的代码来实现我的目标?

c# .net apple-push-notifications sslstream
1个回答
0
投票

我想当你有TCP客户端连接(而不是connectAsync)时,你可以指定一个IPEndPoint对象并使用它来设置标头。

TCP客户端连接:https://docs.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://docs.microsoft.com/en-us/dotnet/api/system.net.ipendpoint?view=netframework-4.7.2

我从来没有尝试过这样的帖子,所以不确定你的请求是什么JSON。

© www.soinside.com 2019 - 2024. All rights reserved.