How about, I have a problem, I try to send notifications from php to ios, I already did all the steps and testing it with an APN Tester program if everything goes well, when I try to send from PHP I get it sent correctly but do not reach the iPhone, what can it be?
This is the code I use:
<?php
// Nuestro token
$deviceToken = 'el token del dispositivo';
// El password del fichero .pem
$passphrase = 'la password 123';
// El mensaje push
$message = '¡Mi primer mensaje Push!';
$ctx = stream_context_create();
//Especificamos la ruta al certificado .pem que hemos creado
stream_context_set_option($ctx, 'ssl', 'local_cert', 'archivo.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Abrimos conexión con APNS
$fp = stream_socket_client(
'ssl://gateway.sandbox.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp) {
exit("Error de conexión: $err $errstr" . PHP_EOL);
}
echo 'Conectado al APNS' . PHP_EOL;
// Creamos el payload
$body['aps'] = array(
'alert' => $message,
'sound' => 'default',
'badge' => 35
);
// Lo codificamos a json
$payload = json_encode($body);
// echo $deviceToken;
// Construimos el mensaje binario
$msg = chr(0) . pack('n', 32) . pack('H*',sprintf('%u', CRC32($deviceToken))) . pack('n', strlen($payload)) . $payload;
// Lo enviamos
$result = fwrite($fp, $msg, strlen($msg));
if (!$result) {
echo 'Mensaje no enviado' . PHP_EOL;
} else {
echo 'Mensaje enviado correctamente' . PHP_EOL;
}
// cerramos la conexión
fclose($fp);
?>