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;
<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 PHP Framework实用入门教程(详细介绍)
- Yii操作数据库的3种方法
- 在yii中新增一个用户验证的方法详解
- Yii框架在页面输出执行sql语句以方便调试的实现方法
- 浅析Yii中使用RBAC的完全指南(用户角色权限控制)
- yii中widget的用法
- Yii实现自动加载类地图的方法
- Yii配置文件用法详解
- yiic命令时提示“php.exe”不是内部或外部命令的解决方法
- Yii扩展组件编写方法实例分析
- Yii获取当前url和域名的方法
- Yii核心组件AssetManager原理分析
- yii操作session实例简介
- Yii使用ajax验证显示错误messagebox的解决方法
- Yii中使用PHPExcel导出Excel的方法
- Yii入门教程之Yii安装及hello world
- Yii学习总结之安装配置
- yii实现图片上传及缩略图生成的方法
- yii实现创建验证码实例解析
- Yii调试SQL的常用方法