ios does not receive push notifications

0

The problem is this, in my app the server sends notifications to the users but I do not know why the devices are not receiving the notifications.

What I've tested:

Server side sends the notifications without problems (active and correct certificates) and tokenID correct and active in the device.

I used the APN Tester Free application to test sending notifications in Production with the same certificate and in this case it sends and the devices receive the notifications.

What could be wrong if you can not receive the notifications?

    
asked by anonymous 19.08.2016 / 16:30

1 answer

1

Try this code in C #:

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