How to build a simple APNS server with PHP?

Asked

Viewed 174 times

-2

I am creating a small service triggering notifications.

I made this code, to make several pushes shots, where the devicetokens come from a server. But I’m finding the following problems:

When I directly assign a token to the array $deviceToken, push normally reaches the smartphone.

When the token comes from the server, the successful sending message appears, but the push does not reach the smartphone.

Code:

<?php

include "conexao.php";

$sql="SELECT `devicetoken` FROM `devicetokensios`  ORDER BY `index`";

$resultado = mysql_query($sql) or die ("Erro .:" . mysql_error());

$deviceToken = array();


// Passando tokens para o array
  while($r = mysql_fetch_assoc($resultado))
{
     $deviceToken [] = $r['devicetoken'];


}

// Coloque sua senha do certificado aqui:
$passphrase = '';

// Coloque sua mensagem aqui:
$message = 'Teste de novas mensagens!!!';

////////////////////////////////////////////////////////////////////////////////


$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'dev30.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

// Abre uma conexão com o servidor 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("Falha para conectar: $err $errstr" . PHP_EOL);

echo 'Conectado a APNS' . PHP_EOL;

// Cria o corpo do payload
$body['aps'] = array(
    'alert' => $message,
    'sound' => 'default'
    );

// Codifica o payload como JSON
$payload = json_encode($body);

// Looping Principal de Envio
for($idx = 0; $idx< count($deviceToken);$idx++){

// Construindo o binário da notificação
$msg = chr(0).pack('n', 32).pack('H*',$deviceToken[$idx]).pack('n', strlen($payload)).$payload;


// Enviando para o servidor
$result = fwrite($fp, $msg, strlen($msg));

if (!$result)
    echo 'Mensagem não enviada' . PHP_EOL;
else
    echo 'Mensagem enviada com sucesso.  ||  ' . PHP_EOL;

// tempo para intervalo entre mensagens
usleep(1000000); 
}

// Fecha conexão com os servidores
mysql_close();
fclose($fp);

?>

On the server the column devicetoken is with the following definitions:

char(64), utf8_general_ci

An important observation, when I manually put several Vices (in this case several times) in the array, I can make the sending tbm looping, sending several times. But the intention is that they be for several different.

  • I was watching that now.

  • I am not "expert" in PHP, my strong is another language. But I need to have a PHP server for testing. So I’m starting now, in a deeper way, in PHP

1 answer

0


I noticed that there was a loss of the last digits of the devicetokens on the server due to the length of the string, so I adjusted the column to a new data length and inserted the device token back into the server. After tests using the database as the source of the tokens I was able to verify that it was solved.

I was able to solve the problem by adjusting the server to the following settings:

varchar(64), utf8_bin

Browser other questions tagged

You are not signed in. Login or sign up in order to post.