您的位置:首页 > 编程语言 > PHP开发

Yii 1.x IOS推送

2015-08-05 20:20 573 查看
获得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;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  yii