thinkphp5接入极光推送的实现方式

composer安装

进入PHP项目目录,执行

composer require jpush/jpush

安装成功后tp5测试


namespace app\jpush\controller;

class IndexController
{
     
    static $app_key="";
    static $master_secret="";
    /*
     * 测试推送
     */
    public function test() {
     
        $app_key=self::$app_key;
        $master_secret=self::$master_secret;
        $client = new \JPush\Client($app_key,$master_secret);
        $pusher = $client->push();
        $pusher->setPlatform('all');
        $pusher->addAllAudience();
        $pusher->setNotificationAlert('test,JPush---'.date("Y-m-d H:i:s"));
        try {
     
            $res=$pusher->send();
            var_dump($res);
        } catch (\JPush\Exceptions\JPushException $e) {
     
            print $e;
        }
    }
}

git clone下载至 path\vendor

require引入autoload.php文件


namespace app\portal\controller;


require_once VENDOR_PATH."jpush/jpush/autoload.php";

class IndexController
{
     
    static $app_key="";
    static $master_secret="";
    
    public function test() {
     
        $app_key=self::$app_key;
        $master_secret=self::$master_secret;
        $client = new \JPush\Client($app_key,$master_secret);
        $pusher = $client->push();
        $pusher->setPlatform('all');
        $pusher->addAllAudience();
        $pusher->setNotificationAlert('test,JPush---'.date("Y-m-d H:i:s"));
        try {
     
            $res=$pusher->send();
            var_dump($res);
        } catch (\JPush\Exceptions\JPushException $e) {
     
            print $e;
        }

    }
}

不使用官方的PHPSDK,直接REST api接口调用

public function test(){
     
        $url = "https://bjapi.push.jiguang.cn/v3/push";
        $data['platform'] = 'all';
        $data['audience'] = 'all';
        //message:自定义消息 透传给 App,需要 App 自行处理
        $data['message'] = array(
            'msg_content'=>json_encode(array('id'=>1))
        );
        //通知的内容在各个平台上,都可能只有这一个最基本的属性 "alert"。
        $extras = json_encode(array('id'=>1));
        $data['notification'] = array(
            'android'=>array('alert'=>"test,JPush---".date("Y-m-d H:i:s"), 'extras'=>json_decode($extras, true)),
            'ios'=>array('alert'=>"test,JPush---".date("Y-m-d H:i:s"), 'extras'=>json_decode($extras, true))
        );
        $authup=self::$app_key.":".self::$master_secret;
        $headers[]="Content-Type:application/json";
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);// 参数为1表示输出信息头,为0表示不输出
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);// 参数为1返回,为0表示echo
        curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($curl, CURLOPT_USERPWD, $authup);// Authorization 认证方式
        curl_exec($curl);
        curl_close($curl);
    }

你可能感兴趣的:(php)