Notifications do not reach the device

0

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);
?>
    
asked by Luis Adrian Carpio 29.09.2016 в 23:41
source

2 answers

1

You should check that the .pem certificate is correct, in this case of sandbox and not of distribution, you should also check that the identifier associated with the provisioning profile allows push notifications, in this example they are deactivated:

    
answered by 13.10.2016 в 15:22
0

First you have to see that your certificates are correct, you can also test if the notifications are correctly configured on your device with tools such as: APN Tester link if with your certificate, the push arrives in the tester, it means that you have a problem with php, it can be your device token or that your certificates are not correct, you can verify if you test with platforms like firebase that your certificates match the above and try to send push, greetings!

    
answered by 20.10.2016 в 16:03