Чтобы отправить запрос APN с использованием PHP, вам необходимы следующие требования:
- A
.pem
сертификат, который должен существовать по тому же пути вашего php-скрипта. - УстройствоТокен, который необходим для отправки уведомления на определенное устройство.
Затем вы можете попробовать следующий код:
<?php
$apnsServer = 'ssl://gateway.push.apple.com:2195';
$privateKeyPassword = '1234'; // your .pem private key password
$message = 'Hello world!';
$deviceToken = 'YOUR_DEVICE_TOKEN_HERE';
$pushCertAndKeyPemFile = 'PushCertificateAndKey.pem'; // Your .pem certificate
$stream = stream_context_create();
stream_context_set_option($stream,
'ssl',
'passphrase',
$privateKeyPassword);
stream_context_set_option($stream,
'ssl',
'local_cert',
$pushCertAndKeyPemFile);
$connectionTimeout = 20;
$connectionType = STREAM_CLIENT_CONNECT | STREAM_CLIENT_PERSISTENT;
$connection = stream_socket_client($apnsServer,
$errorNumber,
$errorString,
$connectionTimeout,
$connectionType,
$stream);
if (!$connection){
echo "Failed to connect to the APNS server. Error no = $errorNumber<br/>";
exit;
} else {
echo "Successfully connected to the APNS...";
}
$messageBody['aps'] = array('alert' => $message,
'sound' => 'default',
'badge' => 2,
);
$payload = json_encode($messageBody);
$notification = chr(0) .
pack('n', 32) .
pack('H*', $deviceToken) .
pack('n', strlen($payload)) .
$payload;
$wroteSuccessfully = fwrite($connection, $notification, strlen($notification));
if (!$wroteSuccessfully){
echo "Could not send the message.";
}
else {
echo "Successfully sent the message.";
}
fclose($connection);
?>
См. Эту ссылку для более подробной информации.