极光推送

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了极光推送相关的知识,希望对你有一定的参考价值。

<?php
namespace jpush;
require_once ‘../extend/jpush/autoload.php‘;
use JPushClient 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 (JPushExceptionsAPIConnectionException $e) {
      $ret = [‘code‘=>0,‘data‘=>‘‘,‘msg‘=>$e];
      } catch (JPushExceptionsAPIRequestException $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;
      }
      }

以上是关于极光推送的主要内容,如果未能解决你的问题,请参考以下文章

cordova 极光推送设置别名技巧

极光推送——别名设置

iOS 极光推送诶配置成功却收不到推送消息的处理

iOS极光推送别名限制问题

极光推送服务器端向ios端推送消息需要设置哪些参数

极光推送的自定义消息推送之后ios怎么使用