極光推送 php版

極光推送插件下載地 址:https://docs.jiguang.cn/jpush/resources/#sdk_1

tp5框架使用:

 

1、將官方demo放到項目vendor中,命名爲jpush

2、在api中新建控制器,如下代碼

<?php
namespace app\api\controller;
use JPush\Client as JPush;
use think\facade\Request;
use think\Db;

class AndroidPush extends Common
{
    
    private $master_secret = '';
    private $app_key = '';
    public function push(){
        
        $client = new \JPush\Client($this->app_key, $this->master_secret);
        //設備
        $device = $client->device();
        $cleaner=Db::table("cleaner")->where("jpush_id != ''")->select();
        //推送
        $pusher = $client->push();
        $pusher->setPlatform('all');//所有平臺
        foreach($cleaner as $k=>$v){
            
            $pusher->addRegistrationId($cleaner[$k]['jpush_id']);
            
        }
         $pusher->setNotificationAlert('重要通知:接收到新的訂單了,請到接單大廳查看');
            try {
                $result=$pusher->send();
                print_r($result);
                
            } catch (\JPush\Exceptions\JPushException $e) {
                // try something else here
                print $e;
            }
        die;
        //$device->deleteTag('tag1'); //刪除標籤
        //$pusher->addRegistrationId($string);
        //$device->addTags("140fe1da9e2b7e68d92", 'tag1');
        //$device->addTags("190e35f7e0c749ba695", 'tag1');
        //print_r($device->getDevices("190e35f7e0c749ba695"));die;
        //$device->removeTags("140fe1da9e2b7e68d92",'tags1');
        //print_r($device->getDevices("190e35f7e0c749ba695"));die;
        
        // //$result1 = '{"sendno":"0","msg_id":"2037636937"}';
        // $rA = json_decode($result1, true);
        // return $rA['sendno'] == 0;
        //$pusher = $client->push();
        //$pusher->setPlatform('all');
        //$pusher->addAllAudience();
        
        //$pusher->addRegistrationId("100d855909db60bca5a");
        $pusher->setNotificationAlert('重要通知:接收到新的訂單了,請到接單大廳查看');
      // $exce=new \JPush\Exceptions\JPushException;
        try {
            $result=$pusher->send();
            print_r($result);die;
            $device->deleteTag('tag1');
        } catch (\JPush\Exceptions\JPushException $e) {
            // try something else here
            print $e;
        }
    }

}

3、直接運行push方法就可以

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章