【问题标题】:How do I send Push Notification with "apns-expiration" header using standard C#.NET?如何使用标准 C#.NET 发送带有“apns-expiration”标头的推送通知?
【发布时间】:2019-03-15 15:35:48
【问题描述】:

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

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】:

    我认为当你有 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 是什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-12-29
      • 1970-01-01
      • 2011-06-07
      • 1970-01-01
      • 2012-02-22
      • 2018-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多