php ios推送(代碼)_PHP教程

來源:互聯網
上載者:User
複製代碼 代碼如下:
//php需要開啟ssl(OpenSSL)支援
$apnsCert = "ck.pem";//串連到APNS時的認證許可檔案,認證需格外按要求建立
$pass = "123456";//認證口令
$serverUrl = "ssl://gateway.sandbox.push.apple.com:2195";//push伺服器,這裡是開發測試伺服器
$deviceToken = "a8fcd4aa8943b223d4ebcd54fe168a8b99b3f24c63dbc0612db25a8c0a588675";//ios裝置id,中間不能有空格,每個ios裝置一個id
$message = $_GET ['message'] or $message = "hello!";
$badge = ( int ) $_GET ['badge'] or $badge = 2;
$sound = $_GET ['sound'] or $sound = "default";
$body = array('aps' => array('alert' => $message , 'badge' => $badge , 'sound' => $sound));
$streamContext = stream_context_create();
stream_context_set_option ( $streamContext, 'ssl', 'local_cert', $apnsCert );
stream_context_set_option ( $streamContext, 'ssl', 'passphrase', $pass );
$apns = stream_socket_client ( $serverUrl, $error, $errorString, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $streamContext);//串連伺服器
if ($apns) {
echo "Connection OK
";
} else {
echo "Failed to connect $errorString";
return;
}
$payload = json_encode ( $body );
$msg = chr(0) . pack('n', 32) . pack('H*', str_replace(' ', '', $deviceToken)) . pack('n', strlen($payload)) . $payload;
$result = fwrite ( $apns, $msg);//發送訊息
fclose ( $apns );
if ($result)
echo "Sending message successfully: " . $payload;
else
echo 'Message not delivered';
?>

http://www.bkjia.com/PHPjc/327979.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/327979.htmlTechArticle複製代碼 代碼如下: ?php //php需要開啟ssl(OpenSSL)支援 $apnsCert = "ck.pem";//串連到APNS時的認證許可檔案,認證需格外按要求建立 $pass = "123456";/...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.