獲得IOS的pem文件後 可以對指定的用戶進行推送 IOS的具體推送原理就不詳細贅述了 直接上實現代碼 Windows系統要注意pem文件的路徑
<span style="white-space:pre"> </span>$deviceToken = '';//硬件token
// Put your private key's passphrase here:
$passphrase = '';
// Put your alert message here:
if($type==Constant::MESSAGE_TYPE_GOODS)
$message = "'$senderNickName'評論了您的商品('$title'):'$contents'";
else{
$message = "'$senderNickName'評論了您的求購('$title'):'$contents'";
}
////////////////////////////////////////////////////////////////////////////////
$ctx = stream_context_create();
$path=ROOT_PATH.'/protected/config/ck.pem';
stream_context_set_option($ctx, 'ssl', 'local_cert',$path);
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', $err,
$errstr, 60, STREAM_CLIENT_CONNECT|STREAM_CLIENT_PERSISTENT, $ctx);
if (!$fp)
exit("Failed to connect: $err $errstr" . PHP_EOL);
$report= '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)
$report.='Message not delivered' . PHP_EOL;
else
$report.='Message successfully delivered' . PHP_EOL;
$report.=$message;
// Close the connection to the server
fclose($fp);
return $report;