I’m trying send push notifications, but the APNS send me this response: Resource id #3, so this mean that is missing topic, second apple documentation, right?
What is "topic"? What am I doing wrong?
I create the certificate again, but I think that it is not the problem. I don’t know what is the "topic".
below is my server php:
<?php
$deviceToken = $_POST["deviceToken"];
// Put your private key's passphrase here:
$passphrase = 'password';
// Put your alert message here:
$message = 'My first push notification!';
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);
// Open a connection to the APNS server
$fp = stream_socket_client(
'ssl://gateway.push.apple.com:2195', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
echo $fp;
echo 'Connected to APNS' . PHP_EOL;
// Create the payload body
$body['aps'] = array(
'alert' => $message,
'sound' => 'default'
);
// Encode the payload as JSON
$payload = json_encode($body);
// Build the binary notification
$msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;
// Send it to the server
$result = fwrite($fp, $msg, strlen($msg));
if (!$result)
{
echo 'Message not delivered' . PHP_EOL;
echo $fp;
}
else
{
echo 'Message successfully delivered' . PHP_EOL;
echo $fp;
}
// Close the connection to the server
fclose($fp);
?>
The server sends the message with sucess, but is like I said before… I receive the response of APNS:
Resource id #3.
What can it be?
EDITED
I fix it. The problem was the url… I changed to gateway.sandbox.push.apple.com. That’s URL of Development!
You get the resource error, because you echo a resource.
You do:
In this case $fp is a resource, not a string