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;
}