Java、c# P12證照:http://www.jianshu.com/p/4282…
Php pem證照製作:http://blog.csdn.net/dztianyu…
有可能出現的錯誤:
1.執行php push.php時報錯 :
Warning: stream_socket_client(): SSL operation failed with code 1. OpenSSL Error messages:
error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed in /Users/songhua/Desktop/推送證照檔案/Php-Push/push.php on line 30
Warning: stream_socket_client(): Failed to enable crypto in /Users/songhua/Desktop/推送證照檔案/Php-Push/push.php on line 30
Warning: stream_socket_client(): unable to connect to ssl://gateway.sandbox.push.apple.com:2195 (Unknown error) in /Users/songhua/Desktop/推送證照檔案/Php-Push/push.php on line 30
Failed to connect 0
解決方案:
去下載https://entrust2048.bj.bcebos… 閘道器檔案並在push.php檔案里加入stream_context_set_option($ctx,`ssl`,`cafile`,`entrust_2048_ca.cer`);
php推送程式碼,entrust_2048_ca.cer、pem和php必須在同一個目錄下:
<?php
//手機註冊應用返回唯一的deviceToken
// $deviceToken = `c112ed059d3bc019833f9eb991cd9e594583381dd979b7a6f809f683efd70d82`;
$deviceToken = `0191498cf8704bf9c09a624855f0ad0e0b65933d33e9b296223745b22d8779c8`;
//ck.pem通關密碼
$pass = `123456`;
//訊息內容
$message = `A test message!`;
//badge我也不知是什麼
$badge = 4;
//sound我也不知是什麼(或許是推送訊息到手機時的提示音)
$sound = `Duck.wav`;
//建設的通知有效載荷(即通知包含的一些資訊)
$body = array();
$body[`id`] = "4f94d38e7d9704f15c000055";
$body[`aps`] = array(`alert` => $message);
if ($badge)
$body[`aps`][`badge`] = $badge;
if ($sound)
$body[`aps`][`sound`] = $sound;
//把陣列資料轉換為json資料
$payload = json_encode($body);
echo strlen($payload),"
";
//下邊的寫法就是死寫法了,一般不需要修改,
//唯一要修改的就是:ssl://gateway.sandbox.push.apple.com:2195這個是沙盒測試地址,ssl://gateway.push.apple.com:2195正式釋出地址
$ctx = stream_context_create();
// $pem = dirname(__FILE__) .`/`.`phpck.pem`;
stream_context_set_option($ctx, `ssl`, `local_cert`, `phpck.pem`);
stream_context_set_option($ctx,`ssl`,`cafile`,`entrust_2048_ca.cer`);
stream_context_set_option($ctx, `ssl`, `passphrase`, $pass);
$fp = stream_socket_client(`ssl://gateway.sandbox.push.apple.com:2195`, $err, $errstr, 60, STREAM_CLIENT_CONNECT, $ctx);
if (!$fp) {
print "Failed to connect $err $errstr
";
return;
}
else {
print "Connection OK
<br/>";
}
// send message
$msg = chr(0) . pack("n",32) . pack(`H*`, str_replace(` `, ``, $deviceToken)) . pack("n",strlen($payload)) . $payload;
print "Sending message :" . $payload . "
";
fwrite($fp, $msg);
fclose($fp);
?>