从PHP发送推送通知时警告pack()

时间:2012-01-31 04:20:18

标签: php push-notification

我正在从PHP向iPhone发送推送通知。

连接已成功建立,但它会给我一个错误:

Connection Established

Warning: pack() [function.pack]: Type i: too few arguments in C:\xampp\htdocs\BiiMe\checkAvailableProduct.php on line 68
M{"aps":{"alert":"BiiMe finds product for you","badge":"3","sound":"default"}} M{"aps":{"alert":"BiiMe finds product for you","badge":"3","sound":"default"}}
Warning: socket_close(): supplied resource is not a valid Socket resource in C:\xampp\htdocs\BiiMe\checkAvailableProduct.php on line 79 

我的代码是:

function pushToIphone($deviceToken, $badge){
    ini_set('display_errors','on');
    error_reporting(E_ALL);



//  $apnsHost = 'gateway.sandbox.push.apple.com';
$apnsHost = 'gateway.push.apple.com';
    $apnsPort = 2195;
    $pem_path = dirname(__FILE__);


    $pem_path = $pem_path .'\cert';

    $apnsCert = $pem_path.'\apns_cer.pem';
    echo $apnsCert."<br/>"; 
    $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/>";

        $payload = array();
        $payload['aps'] = array('alert' => 'BiiMe finds product for you', 'badge' => $badge, 'sound' => 'default');
        //$payload['server'] = array('serverId' => $serverId, 'name' => $serverName);
        $payload = json_encode($payload);

        apnsMessage = chr(0) . chr(0) . chr(32) . pack('H*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;

        //$apnsMessage = chr(0) . chr(0) . chr(32) . pack('nic*', str_replace(' ', '', $deviceToken)) . chr(0) . chr(strlen($payload)) . $payload;
        echo $apnsMessage;
        print_r($apnsMessage);


        fwrite($apns, $apnsMessage);    
    } else {
        echo "Connection fail<br/>";
    }

//socket_close($apns);
    socket_close($apns);
    fclose($apns);
}

2 个答案:

答案 0 :(得分:1)

看起来生成给定错误消息的行已在您的代码中注释掉。

Warning: pack() [function.pack]: Type i: too few arguments

表示它在格式字符串中达到了“i”,但没有更多参数可以格式化。格式字符串nic*至少需要3个参数,但您只提供了一个参数。

您已取消注释的代码看起来只要$ deviceToken是十六进制字符串就可以正常工作。

答案 1 :(得分:0)

它刚刚来,因为我发送了错误的设备令牌它只需要数字,字符和空格。禁止使用特殊字符。

socket_close();不要使用。

Use fclose(apns);代替socket_close(apns);