c# - 如何在不排队的情况下从 C# 发送 APNS 推送通知 (iOS)

标签 c# ios push-notification apple-push-notifications pushsharp

似乎每个人都使用 PushSharp 从 C# 向 iOS 设备发送推送通知。但是那个库有一个它使用的队列而不是直接发送通知,这意味着你需要一个 Windows 服务或其他东西来正确地托管它(根据它自己的文档),这对我来说太过分了。我有一个传入的 Web 请求到我的 ASP.NET Web 服务,作为处理的一部分,我想立即发送推送通知。就这么简单。

谁能告诉我如何使用 PushSharp 立即发送推送通知(绕过其队列机制)或如何正确地自己发送推送通知?我已经有了制定 JSON 消息的代码,但我不知道如何将 .p12 文件应用于请求。我找不到有关如何执行此操作的任何 Apple 文档。

最佳答案

这是一个老问题,但答案并不完整。

这是我的代码:

// private fields
private static readonly string _apnsHostName = ConfigurationManager.AppSettings["APNS:HostName"];
private static readonly int _apnsPort = int.Parse(ConfigurationManager.AppSettings["APNS:Port"]);
private static readonly string _apnsCertPassword = ConfigurationManager.AppSettings["APNS:CertPassword"];
private static readonly string _apnsCertSubject = ConfigurationManager.AppSettings["APNS:CertSubject"];
private static readonly string _apnsCertPath = ConfigurationManager.AppSettings["APNS:CertPath"];

private readonly ILogger _log;

private X509Certificate2Collection _certificatesCollection;


ctor <TAB key>(ILogger log)
{
    _log = log ?? throw new ArgumentNullException(nameof(log));

    // load .p12 certificate in the collection
    var cert = new X509Certificate2(_apnsCertPath, _apnsCertPassword);
    _certificatesCollection = new X509Certificate2Collection(cert);
}

public async Task SendAppleNativeNotificationAsync(string payload, Registration registration)
{
    try
    {
        // handle is the iOS device Token
        var handle = registration.Handle;

        // instantiate new TcpClient with ApnsHostName and Port
        var client = new TcpClient(_apnsHostName, _apnsPort);

        // add fake validation
        var sslStream = new SslStream(client.GetStream(), false, new RemoteCertificateValidationCallback(ValidateServerCertificate), null);

        try
        {
            // authenticate ssl stream on ApnsHostName with your .p12 certificate
            sslStream.AuthenticateAsClient(_apnsHostName, _certificatesCollection, SslProtocols.Tls, false);
            var memoryStream = new MemoryStream();
            var writer = new BinaryWriter(memoryStream);
            // command
            writer.Write((byte)0);
            // first byte of the deviceId length (big-endian first byte)
            writer.Write((byte)0);
            // deviceId length (big-endian second byte)
            writer.Write((byte)32);
            // deviceId data (byte[])
            writer.Write(HexStringToByteArray(handle.ToUpper()));
            // first byte of payload length; (big-endian first byte)
            writer.Write((byte)0);
            // payload length (big-endian second byte)
            writer.Write((byte)Encoding.UTF8.GetByteCount(payload));
            byte[] b1 = Encoding.UTF8.GetBytes(payload);
            // payload data (byte[])
            writer.Write(b1);

            writer.Flush();
            byte[] array = memoryStream.ToArray();

            await sslStream.WriteAsync(array, 0, array.Length);

            // TIP: do not wait a response from APNS because APNS return a response only when an error occurs; 
            // so if you wait the response your code will remain stuck here.
            // await ReadTcpResponse();

            sslStream.Flush();

            // close client
            client.Close();
        }
        catch (AuthenticationException ex)
        {
            _log.Error($"Error sending APNS notification. Exception: {ex}");
            client.Close();
        }
        catch (Exception ex)
        {
            _log.Error($"Error sending APNS notification. Exception: {ex}");
            client.Close();
        }
    }
    catch (Exception ex)
    {
        _log.Error($"Error sending APNS notification. Exception: {ex}");
    }
}

private static byte[] HexStringToByteArray(string hex)
{
    if (hex == null)
    {
        return null;
    }

    // added for newest devices (>= iPhone 8)
    if (hex.Length % 2 == 1)
    {
        hex = '0' + hex;
    }

    return Enumerable.Range(0, hex.Length)
                     .Where(x => x % 2 == 0)
                     .Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
                     .ToArray();
}

private static bool ValidateServerCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
{
    return true;
    //if (sslPolicyErrors == SslPolicyErrors.None)
    //    return true;

    //// do not allow this client to communicate with unauthenticated servers.
    //return false;
}

private async Task<byte[]> ReadTcpResponse(SslStream sslStream)
{
    MemoryStream ms = new MemoryStream();

    byte[] buffer = new byte[2048];

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

        await ms.WriteAsync(buffer, 0, bytes);

    } while (bytes != 0);

    return ms.ToArray();
}

提示:在 iOS13 中,设备 token 的接收方式不同。

> iOS 12 (deviceToken as NSData).description -> "< your_token_here >"
> iOS 13 (deviceToken as NSData).description -> "{ length = 32, bytes = 0x321e1ba1c1ba...token_in_bytes }"

With iOS13 you must convert token to string or skip the method 'HexStringToByteArray' because you already have a byte[].

如果你有问题,我很乐意回答。

关于c# - 如何在不排队的情况下从 C# 发送 APNS 推送通知 (iOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21472942/

相关文章:

c# - 没有安装程序的 XNA 游戏应用程序

ios - 降低解析服务器 Swift 每月的总请求数

ios - 通过推送通知打开特定的 View Controller

c# - Microsoft StreamInsight 准备好迎接黄金时段了吗?

c# - 查找文本小写或大写

ios - Cordova iOS 构建失败,带有 'edit-config' 标记

ios - 收到适用于 IOS 的 Opencv 库的错误

android - 使用 cordova 推送插件推送带有大图像的通知

ios - TestFlight 推送通知不起作用

c# - 如何更改任务中的值