APN通知失败

时间:2009-09-30 11:16:08

标签: php apple-push-notifications

我尝试了以下代码(PHP)

$apnsHost = 'gateway.sandbox.push.apple.com';
$apnsPort = 2195;
$apnsCert = 'apple_push_notification_production.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);
if($apns)
{
    echo "Connection Established<br/>";
    $deviceToken = '**********';//masked

    $body = array();
    $body['aps'] = array(’alert’ => "test message");
    //$body['aps']['badge'] = 1;

    $payload = json_encode($body);


    $apnsMessage = chr(0) . pack("n",32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack("n",strlen($payload)) . $payload;
    print "sending message :" . $apnsMessage . "<br/>";
    print "sending payload :" . $payload . "<br/>";
    fwrite($apns, $apnsMessage);

}
else
{   
    echo "Connection Failed";

    echo $errorString;
    echo $error;
}
socket_close($apns);
fclose($apns);  

回复是

Connection Established
sending message :�� d^÷Îå0ZCd%1ÄuwOOYš'ÊÈ}ârðm¾Í�,{"aps":{"\u2019alert\u2019":"test message"}}
sending payload :{"aps":{"\u2019alert\u2019":"test message"}}

但我无法收到通知

任何帮助?

1 个答案:

答案 0 :(得分:1)

这只是猜测,但是根据您发布的代码中的文件名,您正在使用针对沙盒推送服务器的生产证书。您不能混合使用沙箱和生产证书,也不能使用设备令牌。