I need to implement apple push notifications with a php server. I think the connection is correctly working but the message is refused by apple server for some reason.
I’m getting this error:
“error 0errorString
\nFatal error: Call to undefined
function socket_close() in
/home/www/76cdd1fbdfc5aca0c9f07f489ecd9188/web/mobileApp/handleRequest.php
on line 420
\n”
This is my simple code (with a fake message, just for testing):
$payload['aps'] = array('alert' => 'This is the alert text', 'badge' => unread, 'sound' => 'default');
$payload['server'] = array('serverId' => $serverId, 'name' => $name);
$payload = json_encode($payload);
$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'ck.pem';
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', $apnsCert);
$apns = stream_socket_client('ssl://' . $apnsHost . ':' . $apnsPort, $error, $errorString, 2, STREAM_CLIENT_CONNECT, $streamContext);
echo "error " . $error;
echo "errorString" . $errorString;
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
fwrite($apns, $apnsMessage);
socket_close($apns);
fclose($apns);
echo "Sent";
thanks
I’ve actually solved. The problem was the content of the message (empty). The socket function works perfectly.