Yii2接口类里面直接调用Jpush的公共方法Demo示例
- 6393
- PHP
- 28
- super_dodo
- 2016/10/11
现在很多APP都使用极光推送实现消息订单等的通知。极光推送的PHP的接口之前的文章已经介绍过。文章底部有相关的链接可以直接访问。此处是写了一个公共的方法在很多地方直接调用。
使用示例如下代码所示,有区分个人版本和企业版本。传入个人(企业)ID和推送的信息内容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | //Jpushe 推送给用户的公共方法 public function jpushMember( $member_id , $msg = '' ){ if (! $member_id ){ file_put_contents ( './runtime/logs/jpush_err_member.txt' , date ( 'Y-m-d H:i:s' ). '--会员ID不存在或者不正确.' .PHP_EOL, FILE_APPEND); } //用户下订单--推送消息给商家 $memberInfo = MemberList::findOne( $member_id ); if (! empty ( $memberInfo ->tk)){ $pushObj = new Jpush(); //组装需要的参数 $receive = array ( 'alias' => array ( $memberInfo ->tk)); //别名 $content = $msg ; $m_type = 'tips' ; $m_txt = $msg ; $m_time = '600' ; //离线保留时间 $result = $pushObj ->push( $receive , $content , $m_type , $m_txt , $m_time ); if ( $result ){ //返回值 $res_arr = json_decode( $result , true); if (isset( $res_arr [ 'error' ])){ //如果返回了error则证明失败 file_put_contents ( './runtime/logs/jpush_err_member.txt' , date ( 'Y-m-d H:i:s' ). '--' . $memberInfo ->name. '--' . $res_arr [ 'error' ][ 'message' ]. $res_arr [ 'error' ][ 'code' ].PHP_EOL, FILE_APPEND); } } else { file_put_contents ( './runtime/logs/jpush_err_member.txt' , date ( 'Y-m-d H:i:s' ). '--' . $memberInfo ->name. '--推送消息失败.接口调用失败或无响应.' .PHP_EOL, FILE_APPEND); } } else { file_put_contents ( './runtime/logs/jpush_err_member.txt' , date ( 'Y-m-d H:i:s' ). '--' . $memberInfo ->name. '--用户TK码不存在或者不正确.' .PHP_EOL, FILE_APPEND); } } //Jpushe 推送给商家的公共方法 public function jpushVip( $vip_id , $msg = '' ){ if (! $vip_id ){ file_put_contents ( './runtime/logs/jpush_err_vip.txt' , date ( 'Y-m-d H:i:s' ). '--商家ID不存在或者不正确.' .PHP_EOL, FILE_APPEND); } //用户下订单--推送消息给商家 $vipInfo = VipInfo::findOne( $vip_id ); if (! empty ( $vipInfo ->tk)){ $pushObj = new JpushVip(); //组装需要的参数 $receive = array ( 'alias' => array ( $vipInfo ->tk)); //别名 $content = $msg ; $m_type = 'tips' ; $m_txt = $msg ; $m_time = '600' ; //离线保留时间 $result = $pushObj ->push( $receive , $content , $m_type , $m_txt , $m_time ); if ( $result ){ //返回值 $res_arr = json_decode( $result , true); if (isset( $res_arr [ 'error' ])){ //如果返回了error则证明失败 file_put_contents ( './runtime/logs/jpush_err_vip.txt' , date ( 'Y-m-d H:i:s' ). '--' . $vipInfo ->username. '--' . $res_arr [ 'error' ][ 'message' ]. $res_arr [ 'error' ][ 'code' ].PHP_EOL, FILE_APPEND); } } else { file_put_contents ( './runtime/logs/jpush_err_vip.txt' , date ( 'Y-m-d H:i:s' ). '--' . $vipInfo ->username. '--推送消息失败.接口调用失败或无响应.' .PHP_EOL, FILE_APPEND); } } else { file_put_contents ( './runtime/logs/jpush_err_vip.txt' , date ( 'Y-m-d H:i:s' ). '--' . $vipInfo ->username. '--商家TK码不存在或者不正确.' .PHP_EOL, FILE_APPEND); } } |
极光推送Jpush(v3)服务端PHP版本的api脚本类
纵使这世间再过繁花似锦,也抵不过你的回眸一笑。
相关阅读
- 通过Google API客户端访问Google Play帐户报告PHP库
- PHP执行文件的压缩和解压缩方法
- 消息中间件MQ与RabbitMQ面试题
- 如何搭建一个拖垮公司的技术架构?
- Yii2中ElasticSearch的使用示例
热门文章
- 通过Google API客户端访问Google Play帐户报告PHP库
- PHP执行文件的压缩和解压缩方法
- 消息中间件MQ与RabbitMQ面试题
- 如何搭建一个拖垮公司的技术架构?
- Yii2中ElasticSearch的使用示例
最新文章
- 通过Google API客户端访问Google Play帐户报告PHP库
- PHP执行文件的压缩和解压缩方法
- 消息中间件MQ与RabbitMQ面试题
- 如何搭建一个拖垮公司的技术架构?
- Yii2中ElasticSearch的使用示例