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

Asked

Viewed 124 times

1

I can already send push notification to Android. Taking into account that I already own all keys, device token and certificates, how is it possible to send a push notification to iOS?

I saw 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) on the Firebase website as image below: Configuração do app para iOS no Firebase

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

  • I also had this problem at first, I went to see and it was the certificates and the build of the app, I had used phonegap and openssl to generate the certificates... When passing the app to mac and I exported the certificates there, everything was solved... I think apple must have changed something and now it is only possible to do this with a mac

  • My certificates are correct. So much so that I have already done a test on the site http://pushtry.com/ and it worked.

  • Dude, I don’t know if it helps or if you know, but just take a look: http://caiquedourado.com.br/gui-notificationspush-para-ecommerce/ or right on the site: onesignal.com but if you want to create it yourself, then I’m sorry about that answer, but I think it might give you an idea of how to do it, maybe.

  • @Jádercarvalhodemedeiros and tried to push for iOS? You received the notification when it was through the site?

  • @Joãosilva I made the PUSH for iOS through the site I mentioned and it worked yes. I received it correctly on the device.

  • @Lucascarvalho It doesn’t help because they are examples of browser PUSH and are outsourced Apis. I need to communicate directly with Apns (Apple Push Notification service).

  • Install Postman and post with this data: https://pastebin.com/rD6u0a8S below says if it was successfull or not and if it wasn’t error.

Show 2 more comments

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;
}
  • @diegofm I used the answer area to post my answer. While waiting for someone from the community to help me, fortunately I found the solution and decided to publish it to share with colleagues from the community.

  • Jader, oh yes, forgive me, I thought it was a complement :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.