无法建立连接:尝试在IOS上发送通知时出错0

时间:2016-11-28 12:50:29

标签: php ios push-notification apple-push-notifications

我正在尝试使用php发送IOS应用程序的推送通知。但是,当我尝试建立连接时,但每当我尝试建立连接时,我得到一个"无法建立连接:0"错误。谁能告诉我我的代码有什么问题。

function iOS()
{
    $deviceToken ="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";

    $ctx = stream_context_create();
    $pem = get_site_url().'/wp-content/themes/MY-child/ck.pem';
    $passphrase = '';

    stream_context_set_option($ctx, 'ssl', 'local_cert', $pem);
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

    $fp = stream_socket_client(
        'ssl://gateway.sandbox.push.apple.com:2195', $err,
        $errstr, 60, STREAM_CLIENT_CONNECT , $ctx);
    if (!$fp)
        exit("Failed to establish connection : $err $errstr" . PHP_EOL);


    $body['aps'] = array(
        'alert' => "Hello from APNs Tester",
        'sound' => 'default'
    );

    $payload = json_encode($body);

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

    $result = fwrite($fp, $msg, strlen($msg));
    fclose($fp);

    if (!$result)
        return 'Message not delivered' . PHP_EOL;
    else
        return 'Message successfully delivered' . PHP_EOL;
}

0 个答案:

没有答案