How to do Push Notification for iOS? (preferably using C #)

1

I can now send push notification for Android. Given that I already have all the device keys, token, and certificates, how can I send a push notification to iOS?

I've seen in some places that the payload for iOS is different from Android. I am using GCM (Google Cloud Messaging) and configured the account (with the certificates) in the Firebase site as below:

Could someone help me send a push notification to iOS device please? If you know in C #, better yet, but it can be in any language that already helps.

    
asked by anonymous 13.06.2017 / 12:07

1 answer

0

I solved the problem using this code in C # below:

public static void EnviarNotificacaoPush(string tokeDispositivo, string mensagem, string caminhoCertificado, string senhaCertificado)
{
    using (var certificado = new X509Certificate2(File.ReadAllBytes(caminhoCertificado), senhaCertificado))
    {
        var enderecoPushApple = certificado.FriendlyName.StartsWith("Apple Development") ? "gateway.sandbox.push.apple.com" : "gateway.push.apple.com";
        using (var clientTcp = new TcpClient(enderecoPushApple, 2195))
        {
            using (var sslStream = new SslStream(clientTcp.GetStream()))
            {
                sslStream.AuthenticateAsClient(enderecoPushApple, new X509Certificate2Collection(certificado), SslProtocols.Default, false);
                using (var memoryStream = new MemoryStream())
                {
                    using (var writer = new BinaryWriter(memoryStream))
                    {
                        var payload = JsonConvert.SerializeObject(new { aps = new { alert = mensagem } });
                        writer.Write(new byte[] { 0, 0, 32 }.Concat(HexToData(tokeDispositivo)).Concat(new byte[] { 0, (byte)payload.Length }).ToArray());
                        writer.Write(payload.ToCharArray());
                        writer.Flush();
                        sslStream.Write(memoryStream.ToArray());
                        sslStream.Flush();
                    }
                }
            }
        }
    }
}

private static byte[] HexToData(string hexString)
{
    if (hexString.Length % 2 == 1)
        hexString = '0' + hexString;
    var 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;
}
    
13.06.2017 / 17:26