码迷,mamicode.com
首页 > 其他好文 > 详细

极光推送

时间:2020-04-20 23:24:22      阅读:135      评论:0      收藏:0      [点我收藏+]

标签:开发人员   cli   一个   必须   唯一性   联系   号码   错误   不清楚   

<?php
namespace jpush;
require_once ‘../extend/jpush/autoload.php‘;
use JPush\Client as JgPush;
/**

  • 极光推送
    */
    class jpush{
    private $app_key = ‘56df4a96d1dea893ddb2ad0b‘; // 待发送的应用程序(appKey),只能填一个。
    private $master_secret = ‘265fc9ee9e823dfef610e07f‘; // 主密码

    //若实例化的时候传入相应的值则按新的相应值进行
    public function __construct($app_key=null, $master_secret=null) {
    if ($app_key) $this->app_key = $app_key;
    if ($master_secret) $this->master_secret = $master_secret;
    }
    /**

    • 发送推送信息
    • [send_msg description]
    • @Author 念天地之悠悠
    • @DateTime 2019-12-30
    • @param [type] $title [description] 标题
    • @param [type] $alert [description] 内容
    • @param array $extras [description] 扩展字段
    • @param [type] $type [description] 类型 1 指定用户 2 按别名 3 按标签
    • @param [type] $value [description] 用户信息
    • @param array $platform [description] 推送平台
    • @return [type] [description]
      */
      public function send_msg($title,$alert,$extras=array(),$type,$value,$platform=array(‘android‘,‘ios‘)){
      // 完整的推送示例,包含指定Platform,指定Alias,Tag,指定iOS,Android notification,指定Message等
      $client = new JgPush($this->app_key, $this->master_secret);
      $android_notification = array(
      ‘title‘ => $title,
      ‘builder_id‘ => 2,
      ‘extras‘ => $extras
      );
      $message = array(
      ‘title‘ => $title,
      ‘extras‘ => $extras
      );
      $ios_notification = array(
      ‘badge‘ => 2, // 应用角标 为 0 表示清除
      ‘content-available‘ => true, // 推送唤醒 默认 true
      ‘extras‘ => $extras
      );
      $push = $client->push();
      if ($type == 1) {
      $push_payload = $push
      ->setPlatform($platform)
      ->addRegistrationId($value)
      ->androidNotification($alert,$android_notification)
      ->iosNotification($alert,$ios_notification)
      ->message($alert, $message);
      }elseif ($type == 2) {
      $push_payload = $push
      ->setPlatform($platform)
      ->addAlias($value)
      ->androidNotification($alert,$android_notification)
      ->iosNotification($alert,$ios_notification)
      ->message($alert, $message);
      }elseif ($type == 3) {
      $push_payload = $push
      ->setPlatform($platform)
      ->addTag($value)
      ->androidNotification($alert,$android_notification)
      ->iosNotification($alert,$ios_notification)
      ->message($alert, $message);
      }
      try {
      $response = $push_payload->send();
      $msg = $this->get_msg($response[‘http_code‘]);
      if ($response[‘http_code‘] == 200) {
      $ret = [‘code‘=>1,‘data‘=>$response[‘body‘],‘msg‘=>$msg];
      }else{
      $ret = [‘code‘=>0,‘data‘=>‘‘,‘msg‘=>$msg];
      }
      }catch (\JPush\Exceptions\APIConnectionException $e) {
      $ret = [‘code‘=>0,‘data‘=>‘‘,‘msg‘=>$e];
      } catch (\JPush\Exceptions\APIRequestException $e) {
      $ret = [‘code‘=>0,‘data‘=>‘‘,‘msg‘=>$e];
      }
      return $ret;
      }
      /**
    • 获取返回信息
    • [get_msg description]
    • @Author 念天地之悠悠
    • @DateTime 2019-12-30
    • @param [type] $code [description] 返回状态码
    • @return [type] [description]
      */
      public function get_msg($code){
      switch ($code) {
      case 200:
      $message = ‘发送成功!‘;
      break;
      case 1000:
      $message = ‘失败(系统内部错误)‘;
      break;
      case 1001:
      $message = ‘失败(只支持 HTTP Post 方法,不支持 Get 方法)‘;
      break;
      case 1002:
      $message = ‘失败(缺少了必须的参数)‘;
      break;
      case 1003:
      $message = ‘失败(参数值不合法)‘;
      break;
      case 1004:
      $message = ‘失败(验证失败)‘;
      break;
      case 1005:
      $message = ‘失败(消息体太大)‘;
      break;
      case 1008:
      $message = ‘失败(appkey参数非法)‘;
      break;
      case 1020:
      $message = ‘失败(只支持 HTTPS 请求)‘;
      break;
      case 1030:
      $message = ‘失败(内部服务超时)‘;
      break;
      default:
      $message = ‘失败(返回其他状态,目前不清楚额,请联系开发人员!)‘;
      break;
      }
      return $message;
      }
      /**
    • 查询设备的标签与别名及手机号
    • [getDevices description]
    • @Author 念天地之悠悠
    • @DateTime 2019-12-30
    • @param [type] $registration_id [description] 设备标识
    • @return [type] [description]
      */
      public function getDevices($registration_id){
      $client = new JgPush($this->app_key, $this->master_secret);
      $device = $client->device();
      $info = $device->getDevices($registration_id);
      if ($info[‘http_code‘] == 200) {
      $ret = [‘code‘=>1,‘data‘=>$info[‘body‘],‘msg‘=>‘获取成功!‘];
      }else{
      $ret = [‘code‘=>0,‘data‘=>‘‘,‘msg‘=>‘获取失败!‘];
      }
      return $ret;
      }
      /**
    • 更新别名(具有唯一性)
    • [updateAlias description]
    • @Author 念天地之悠悠
    • @DateTime 2019-12-31
    • @param [type] $registration_id [description] 设备标识
    • @param [type] $alias [description] 别名
    • @return [type] [description]
      */
      public function updateAlias($registration_id,$alias){
      $client = new JgPush($this->app_key, $this->master_secret);
      $device = $client->device();
      $info = $device->updateAlias($registration_id,$alias);
      if ($info[‘http_code‘] == 200) {
      $ret = [‘code‘=>1,‘data‘=>‘‘,‘msg‘=>‘更新成功!‘];
      }else{
      $ret = [‘code‘=>0,‘data‘=>‘‘,‘msg‘=>‘更新失败!‘];
      }
      return $ret;
      }
      /**
    • 新增标签
    • [addTags description]
    • @Author 念天地之悠悠
    • @DateTime 2019-12-31
    • @param [type] $registration_id [description] 设备标识
    • @param [type] $tags [description] 别名 多个用数组 单个用字符串
      */
      public function addTags($registration_id,$tags){
      $client = new JgPush($this->app_key, $this->master_secret);
      $device = $client->device();
      $info = $device->addTags($registration_id,$tags);
      if ($info[‘http_code‘] == 200) {
      $ret = [‘code‘=>1,‘data‘=>‘‘,‘msg‘=>‘新增成功!‘];
      }else{
      $ret = [‘code‘=>0,‘data‘=>‘‘,‘msg‘=>‘新增失败!‘];
      }
      return $ret;
      }
      /**
    • 移除标签
    • [removeTags description]
    • @Author 念天地之悠悠
    • @DateTime 2019-12-31
    • @param [type] $registration_id [description] 设备标识
    • @param [type] $tags [description] 别名 多个用数组 单个用字符串
    • @return [type] [description]
      */
      public function removeTags($registration_id,$tags){
      $client = new JgPush($this->app_key, $this->master_secret);
      $device = $client->device();
      $info = $device->addTags($registration_id,$tags);
      if ($info[‘http_code‘] == 200) {
      $ret = [‘code‘=>1,‘data‘=>‘‘,‘msg‘=>‘移除成功!‘];
      }else{
      $ret = [‘code‘=>0,‘data‘=>‘‘,‘msg‘=>‘移除失败!‘];
      }
      return $ret;
      }
      /**
    • 删除所有标签
    • [clearTags description]
    • @Author 念天地之悠悠
    • @DateTime 2019-12-31
    • @param [type] $registration_id [description] 设备标识
    • @return [type] [description]
      */
      public function clearTags($registration_id){
      $client = new JgPush($this->app_key, $this->master_secret);
      $device = $client->device();
      $info = $device->clearTags($registration_id);
      if ($info[‘http_code‘] == 200) {
      $ret = [‘code‘=>1,‘data‘=>‘‘,‘msg‘=>‘移除成功!‘];
      }else{
      $ret = [‘code‘=>0,‘data‘=>‘‘,‘msg‘=>‘移除失败!‘];
      }
      return $ret;
      }
      /**
    • 绑定手机号码
    • [updateMoblie description]
    • @Author 念天地之悠悠
    • @DateTime 2019-12-31
    • @param [type] $registration_id [description] 设备标识
    • @param [type] $tel [description] 手机号码
    • @return [type] [description]
      */
      public function updateMoblie($registration_id,$tel){
      $client = new JgPush($this->app_key, $this->master_secret);
      $device = $client->device();
      $info = $device->updateMoblie($registration_id,$tel);
      if ($info[‘http_code‘] == 200) {
      $ret = [‘code‘=>1,‘data‘=>‘‘,‘msg‘=>‘绑定成功!‘];
      }else{
      $ret = [‘code‘=>0,‘data‘=>‘‘,‘msg‘=>‘绑定失败!‘];
      }
      return $ret;
      }
      /**
    • 取消手机绑定
    • [clearMobile description]
    • @Author 念天地之悠悠
    • @DateTime 2019-12-31
    • @param [type] $registration_id [description]
    • @return [type] [description]
      */
      public function clearMobile($registration_id){
      $client = new JgPush($this->app_key, $this->master_secret);
      $device = $client->device();
      $info = $device->clearMobile($registration_id);
      if ($info[‘http_code‘] == 200) {
      $ret = [‘code‘=>1,‘data‘=>‘‘,‘msg‘=>‘取消绑定成功!‘];
      }else{
      $ret = [‘code‘=>0,‘data‘=>‘‘,‘msg‘=>‘取消绑定失败!‘];
      }
      return $ret;
      }
      }

极光推送

标签:开发人员   cli   一个   必须   唯一性   联系   号码   错误   不清楚   

原文地址:https://blog.51cto.com/13346331/2488864

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!