iOS推送通知服务器配置

时间:2015-01-05 07:26:34

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

这是我推送通知的php代码。我已经生成了.pem文件,它在从localhost执行“php push.php”时有效。我把它放到了实时网站,当我执行这个脚本时“发生101超时错误。我把.pem文件和这个脚本放在同一个文件夹中。我错过了什么服务器端设置?你能不能帮助我这个?

  <?php

    // Put your device token here (without spaces):
    $deviceToken = //Device Token 

    // Put your private key's passphrase here:
    $passphrase = //Passphrase

    // Put your alert message here:
    $message = 'My first push notification!';


    $ctx = stream_context_create();
    stream_context_set_option($ctx, 'ssl', 'local_cert', 'ck.pem');
    stream_context_set_option($ctx, 'ssl', 'passphrase', $passphrase);

    // Open a connection to the APNS server
    $fp = stream_socket_client(
        'ssl://gateway.sandbox.push.apple.com:2195', $err,
        $errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);

    if (!$fp)
        exit("Failed to connect: $err $errstr" . PHP_EOL);

    echo 'Connected to APNS' . PHP_EOL;

    // Create the payload body
    $body['aps'] = array(
        'alert' => $message,
        'sound' => 'default'
        );

    // Encode the payload as JSON
    $payload = json_encode($body);

    // Build the binary notification
    $msg = chr(0) . pack('n', 32) . pack('H*', $deviceToken) . pack('n', strlen($payload)) . $payload;

    // Send it to the server
    $result = fwrite($fp, $msg, strlen($msg));

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

    // Close the connection to the server
    fclose($fp);
    ?>

0 个答案:

没有答案