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;
}
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
– João Silva
My certificates are correct. So much so that I have already done a test on the site http://pushtry.com/ and it worked.
– Jáder Carvalho de Medeiros
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.
– Lucas de Carvalho
@Jádercarvalhodemedeiros and tried to push for iOS? You received the notification when it was through the site?
– João Silva
@Joãosilva I made the PUSH for iOS through the site I mentioned and it worked yes. I received it correctly on the device.
– Jáder Carvalho de Medeiros
@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).
– Jáder Carvalho de Medeiros
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.
– João Silva