【问题标题】:How to send APNS push notification (iOS) from C# without queuing如何在不排队的情况下从 C# 发送 APNS 推送通知(iOS)
【发布时间】:2014-02-23 17:26:52
【问题描述】:

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

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

【问题讨论】:

  • 我确实找到了 Apple 的文档,他们说这是一个 TCP 二进制通道,必须长时间保持打开状态,而不是只为一条消息打开。所以我想排队很重要。 Apple 表示,如果你不这样做,他们会阻止你作为 DoS 攻击。

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


【解决方案1】:

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

这是我的代码:

// 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,设备令牌的接收方式不同。

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

使用 iOS13 必须将 token 转换为字符串或跳过该方法 'HexStringToByteArray' 因为你已经有一个字节[]。

如果您有任何问题,我很乐意回答。

【讨论】:

    【解决方案2】:

    我花了很多时间试图找到一种推送通知的方法,然后我找到了一段代码来为我做到这一点。

    首先确保您正确安装了证书,这里有一个链接可以帮助您。 https://arashnorouzi.wordpress.com/2011/04/13/sending-apple-push-notifications-in-asp-net-%E2%80%93-part-3-apns-certificates-registration-on-windows/

    这是我用来推送通知的代码:

    public static bool ConnectToAPNS(string deviceId, string message)
        {
            X509Certificate2Collection certs = new X509Certificate2Collection();
    
            // Add the Apple cert to our collection
            certs.Add(getServerCert());
    
            // Apple development server address
            string apsHost;
            /*
            if (getServerCert().ToString().Contains("Production"))
                apsHost = "gateway.push.apple.com";
            else*/
            apsHost = "gateway.sandbox.push.apple.com";
    
            // Create a TCP socket connection to the Apple server on port 2195
            TcpClient tcpClient = new TcpClient(apsHost, 2195);
    
            // Create a new SSL stream over the connection
            SslStream sslStream1 = new SslStream(tcpClient.GetStream());
    
            // Authenticate using the Apple cert
            sslStream1.AuthenticateAsClient(apsHost, certs, SslProtocols.Default, false);
    
            PushMessage(deviceId, message, sslStream1);
    
            return true;
        }
    
    private static X509Certificate getServerCert()
        {
            X509Certificate test = new X509Certificate();
    
            //Open the cert store on local machine
            X509Store store = new X509Store(StoreLocation.CurrentUser);
    
            if (store != null)
            {
                // store exists, so open it and search through the certs for the Apple Cert
                store.Open(OpenFlags.ReadOnly);
                X509Certificate2Collection certs = store.Certificates;
    
                if (certs.Count > 0)
                {
                    int i;
                    for (i = 0; i < certs.Count; i++)
                    {
                        X509Certificate2 cert = certs[i];
    
                        if (cert.FriendlyName.Contains("Apple Development IOS Push Services"))
                        {
                            //Cert found, so return it.
                            Console.WriteLine("Found It!");
                            return certs[i];
                        }
                    }
                }
                return test;
            }
            return test;
        }
    
    private static byte[] HexToData(string hexString)
        {
            if (hexString == null)
                return null;
    
            if (hexString.Length % 2 == 1)
                hexString = '0' + hexString; // Up to you whether to pad the first or last byte
    
            byte[] data = new byte[hexString.Length / 2];
    
            for (int i = 0; i < data.Length; i++)
                data[i] = Convert.ToByte(hexString.Substring(i * 2, 2), 16);
    
            return data;
        }
    

    请注意,此代码用于开发证书“Apple Development IOS Push Services”。

    【讨论】:

    • PushMessage 在这种情况下是最重要的!如果这里没有“pushMessage”,那就是大便!
    猜你喜欢
    • 2015-06-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    • 2019-11-15
    • 2015-12-29
    相关资源
    最近更新 更多