php推送通知APNS沙箱

时间:2011-12-06 15:42:31

标签: php apple-push-notifications

我使用自编脚本通过PHP向APNS发送推送通知。我正试图从沙盒得到响应,我总是得到0.这就是推送成功的意思,但即使故意造成错误,我仍然会得到0。如果在deviceToken中输入拼写错误,我应该得到错误= 8但是stil得到0。

问题是这是他第一次使用沙箱来测试推送,我想知道沙箱是否真的发送通知或者只是将其推送到dev / null ...

以下是我使用的一些代码:

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;
}

上次编辑:代码格式修正

1 个答案:

答案 0 :(得分:2)

更改第二功能 //尝试此功能,我做了类似的事情

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;
     }
}