I use a self written script to send push notifications to APNS with PHP. I'm trying to get the response from the sandbox and I always get 0. This is suppose the mean that the push was a success but I still get 0 even when a intentionally cause an error. If a make a typo in the deviceToken, I should get error = 8 but stil get 0.
The thing is this it's he first time I use the sandbox to test the push and I'm wondering if the sandbox really send the notification or simply push it to something like dev/null...
Here's some of the code that I use:
public function sendMessage($deviceToken, $message)
{
$streamContext = stream_context_create();
stream_context_set_option($streamContext, 'ssl', 'local_cert', self::APNSCERT);
$apns = $this->_connectSocket($streamContext);
$payload = array();
$payload['aps'] = array('alert' => utf8_encode($message), 'badge' => 1, 'sound' => 'default');
$payload = json_encode($payload);
$apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
$success=0;
$retry=0;
while($success==0 && $retry<=3)
{
if (!@fwrite($apns, $apnsMessage))
{
var_dumd($this->_error.)
var_dump($this->_errorString);
$apns = $this->_connectSocket($streamContext);
++$retry;
}
else
{
$success=1;
}
}
usleep(2);
fclose($apns);
}
protected function _connectSocket($streamContext)
{
if (!$apns = stream_socket_client('ssl://'.self::APNSHOST.':'.self::APNSPORT, $this->_error, $this->_errorString, 5, STREAM_CLIENT_CONNECT, $streamContext))
{
var_dumd($this->_error.)
var_dump($this->_errorString);
sleep(1);
}
return $apns;
}
last edit: code formatting correction
Change second function //Try this function, I did something similar
protected function _connectSocket($streamContext)
{
if (!$apns = stream_socket_client('ssl://'.self::APNSHOST.':'.self::APNSPORT,
$this->_error, $this->_errorString, 5, STREAM_CLIENT_CONNECT, $streamContext))
{
exit("Failed to connect: $this->_error $this->_errorString" . PHP_EOL);
}
else
{
echo 'Connected to APNS' . PHP_EOL;
return $apns;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.