极光推送

喜夏-厌秋 提交于 2020-01-23 21:39:42

极光推送

class Jpush extends Model
{
    /*
      $receiver="registration_id" : [ "4312kjklfds2", "8914afd2", "45fdsa31" ];
      $receiver="tag" : [ "深圳", "广州", "北京" ]
      $receiver=  "tag" : [ "深圳", "广州" ]
      $receiver= "tag_and" : [ "女", "会员"]
      //自定义类型推送类型

      $m_type = 'http';//推送附加字段的类型
      $m_txt = 'http://www.groex.cn/';//推送附加字段的类型对应的内容(可不填) 可能是url,可能是一段文字。
      $m_time = '86400';//离线保留时间
     *
     * { "platform" : "all" }
     *
     * { "platform" : ["android", "ios"] }
     *  测试成不成功记得看管理平台上面的统计信息,因为之前把apns_production参数设置成了生产环境,调用接口成功,可是却没有用户,后来改为了开发环境,就成功了。
     */
    function jiguang_send($receive, $content, $platform, $m_type, $m_txt, $m_time,$m_userid=0) {

        $appkey = '85e83e2a75936b872dfec5bd'; //AppKey
        $secret = 'fa6c7f1b5ea2adb74d7eb5da'; //Secret
        $postUrl = "https://api.jpush.cn/v3/push";

        $base64 = base64_encode("$appkey:$secret");
        $header = array("Authorization:Basic $base64", "Content-Type:application/json");
        $data = array();
        $data['platform'] = $platform;          //目标用户终端手机的平台类型android,ios,winphone
        $data['audience'] = $receive;      //目标用户

        $data['notification'] = array(
            //统一的模式--标准模式
            "alert" => $content,
            //安卓自定义
            "android" => array(
                "alert" => $content,
                "title" => "",
                "builder_id" => 1,
                "extras" => array("type" => $m_type, "txt" => $m_txt,"userid" => $m_userid)
            ),
            //ios的自定义
            "ios" => array(
                "alert" => $content,
                "badge" => "1",
                "sound" => "default",
                "extras" => array("type" => $m_type, "txt" => $m_txt,"userid" => $m_userid)
            )
        );

        //苹果自定义---为了弹出值方便调测
        $data['message'] = array(
            "msg_content" => $content,
            "extras" => array("type" => $m_type, "txt" => $m_txt,"userid" => $m_userid)
        );

        //附加选项
        $data['options'] = array(
            "sendno" => time(),
            "time_to_live" => $m_time, //保存离线时间的秒数默认为一天
            "apns_production" => 1, //布尔类型   指定 APNS 通知发送环境:0开发环境,1生产环境。或者传递false和true
        );
        $param = json_encode($data);
        //    $postUrl = $this->url;
        $curlPost = $param;

        $ch = curl_init();                                      //初始化curl
        curl_setopt($ch, CURLOPT_URL, $postUrl);                 //抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);                    //设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);            //要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);                      //post提交方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);           // 增加 HTTP Header(头)里的字段
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);        // 终止从服务端进行验证
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        $return_data = curl_exec($ch);                                 //运行curl
        curl_close($ch);

        return $return_data;
    }

}

 

/**
     * --------------------------------------------------------------------------
     * 极光推送
     * @param data 推送内容
     * @param platform 推送平台
     * --------------------------------------------------------------------------
     */
    private function _pushMes($data,$platform){
        //组装需要的参数
        $registration_id = $data['registration_id'];//注册ID设备标识
        $content = $data['content'];//推送内容
        $m_type = $data['m_type'];//推送附加字段的类型1为用户其他用户登陆需要退出
        $m_txt = $data['m_txt'];//推送附加字段的类型对应的内容(可不填) 可能是url,可能是一段文字。
        $type = $data['type'];//1单独推送2全部推送
        if($type==1){
            $receive['registration_id'] = array($registration_id);
            $m_time = 86400;//离线保留时间
        }else{
           // $receive['registration_id'] = array('161a3797c85761e3645');//35966808210190
            $receive='all';
            $m_time = 86400;//离线保留时间
        }

        $jpush = new Jpush;

        $result = $jpush->jiguang_send($receive, $content, $platform, $m_type, $m_txt, $m_time) ;
        if($result){
            $res_arr = json_decode($result, true);

            if(isset($res_arr['error'])){   //如果返回了error则证明失败
                //错误信息 错误码ceh
                $result = array(
                    'flag' => $res_arr['error']['code'],
                    'msg' => $res_arr['error']['message'],
                    'data' => null
                );
                return $result;
            }else{
                //处理成功的推送......
                //可执行一系列对应操作~
                $result = array(
                    'flag' => 1,
                    'msg' => '推送成功',
                    'data' => $res_arr
                );
                return $result;
            }
        }else{      
            //接口调用失败或无响应
            $result = array(
                'flag' => -2,
                'msg' => '接口调用失败或无响应',
                'data' => null
            );
            return $result;
        }
    }

调用 _pushMes方法

$data = array(
      'registration_id' => "",
       'content' => "《".$live_val['title']."》将于30分钟后开播,请及时关注!",
       'm_type' => '3',
       'm_txt' => '直播通知',
        'type' => '2'
 );
$platform = 'all';
//$platform = array('ios','android');
$this->_pushMes($data,$platform);

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!