`Laravel`版`小丑路人社区`改版中,与`Hyperf版小丑路人社区`数据互动,此版本改版中……尚未彻底完结!

Q:

unipush基于`个推`的PHP服务推送

扩展包

composer require getuilaboratory/getui-pushapi-php-client-v2

实例

GeTuiService::getInstance()->push('client_id', '测试标题 - ' . date('Y-m-d H:i:s'), '内容XXXXXX');

代码


class GeTuiService
{
    protected $api;

    public function __construct()
    {
        //创建API,APPID等配置参考 环境要求 进行获取
        $this->api = new \GTClient("https://restapi.getui.com", "appkey", "appid", "masterSecret");
    }

    function micro_time()
    {
        [
            $usec,
            $sec,
        ] = explode(" ", microtime());
        $time = ($sec . substr($usec, 2, 3));
        return $time;
    }

    public function process($title, $content = '', $clickType = 'intent', $time = 0, $data = [], $url = '', $img = '')
    {
        $stt = ['default' => 1];
        //设置推送参数
        $push = new \GTPushRequest();
        $push->setRequestId($this->micro_time());
        $message = new \GTPushMessage();
        $notify = new \GTNotification();
        $channel = new \GTPushChannel();

        //配置推送条件
        $str = new \GTStrategy();
        $str->setDefault(1);
        $str->setHw(1);
        $setting = new \GTSettings();  //定时推送暂无
        $setting->setStrategy($str);
        $push->setSettings($setting);
        $setting->setTtl(3600000);
        // 消息有效期,走厂商消息需要设置该值

        //推送苹果离线通知标题内容
        $alert = new \GTAlert();
        $alert->setTitle($title);
        $alert->setBody($content);
        $aps = new \GTAps();
        //1表示静默推送(无通知栏消息),静默推送时不需要填写其他参数。
        //苹果建议1小时最多推送3条静默消息
        $aps->setContentAvailable(0);
        $aps->setSound("default");
        $aps->setAlert($alert);
        $iosDto = new \GTIos();
        $iosDto->setAps($aps);
        $iosDto->setType("notify");
        $pushChannel = new \GTPushChannel();
        $pushChannel->setIos($iosDto);

        //安卓离线厂商通道推送消息体
        $pushChannel = new \GTPushChannel();
        $androidDTO = new \GTAndroid();
        $ups = new \GTUps();
        $notification1 = new \GTThirdNotification();;
        $notification1->setTitle($title);
        $notification1->setBody($content);
        $ups->setNotification($notification1);
        if ( $clickType == 'none' )  //设置推送类型
        {
            $notification1->setClickType($clickType);
        } elseif ( $clickType == 'payload' || $clickType == 'payload_custom' ) { //自定义消息 打开APP和不打开APP
            $notification1->setClickType($clickType);
            $notification1->setPayload(json_encode($data));
        } elseif ( $clickType == 'url' ) {  //打开URL
            $notification1->setClickType($clickType);
            $notification1->setUrl($url);
        } elseif ( $clickType == 'intent' ) {  //打开特定页面
            $notification1->setClickType($clickType);
            $notification1->setIntent("intent:#Intent;component=包名/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=" . urlencode($title) . ";S.content=" . urlencode($content) . ";S.payload=" . json_encode($data) . ";end");
        } else {
            $notification1->setClickType($clickType);
        }
        //各厂商自有功能单项设置
        if ( $time ) {
            $message->setDuration($time);  //设置推送时间
            $data = json_encode([
                'title'      => $title,
                'content'    => $content,
                'duration'   => $time,
                'importance' => 'HIGH',
                'payload'    => json_encode($data),
            ]);
        } else {
            $data = json_encode([
                'title'   => $title,
                'content' => $content,
                'payload' => json_encode($data),
            ]);
        }
        //$ups->setTransmission($data);
        $androidDTO->setUps($ups);
        $pushChannel->setAndroid($androidDTO);
        $push->setPushChannel($pushChannel);

        //个推在线通道
        $notify->setTitle($title);
        $notify->setBody($content);
        if ( $img ) $notify->setBigImage($img); //推送图片
        //1、intent:打开应用内特定页面 2、url:打开网页地址。3、payload:自定义消息内容启动应用。4、payload_custom:自定义消息内容不启动应用。5、startapp:打开应用首页。6、none:纯通知,无后续动作
        if ( $clickType == 'none' )  //设置推送类型
        {
            $notify->setClickType($clickType);
        } elseif ( $clickType == 'payload' || $clickType == 'payload_custom' ) { //自定义消息 打开APP和不打开APP
            $notify->setClickType($clickType);
            $notify->setPayload(json_encode($data));
        } elseif ( $clickType == 'url' ) {  //打开URL
            $notify->setClickType($clickType);
            $notify->setUrl($url);
        } elseif ( $clickType == 'intent' ) {  //打开特定页面
            $notify->setClickType($clickType);
            $notify->setIntent("intent:#Intent;component=包名/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=" . urlencode($title) . ";S.content=" . urlencode($content) . ";S.payload=" . json_encode($data) . ";end");
        } else {
            $notify->setClickType($clickType);
        }
        $message->setNotification($notify);
        $push->setPushMessage($message);
        /*  if($time){
              $message->setDuration($time);  //设置推送时间
              $data = json_encode(array('title'=>$title,'body'=>$content,'duration'=>$time,'importance'=>'HIGH','payload'=>json_encode($data)));
          }else{
              $data = json_encode(array('title'=>$title,'body'=>$content,'payload'=>json_encode($data)));
          }
          $ups->setTransmission($data);  //
          $android->setUps($ups);
          $channel->setAndroid($android);
          $push->setPushChannel($channel);
          $push->setPushMessage($message);*/
        return $push;
    }

    public function push($client_id, $title, $content = '')
    {
        $push = $this->process($title, $content);
        Log::info('消息推送 - 内容', compact('client_id', 'title', 'content'));

        // 客户标识
        $push->setCid($client_id);
        //处理返回结果
        $result = $this->api->pushApi()->pushToSingleByCid($push);

        Log::info('消息推送 - 结果', $result);

        if ( $result['code'] == 0 ) {
            return true;
        } else {
            return $result['msg'];
        }
    }
}
技术分享
订阅

评论记录

小丑路人

官方composer包有test文件的测试demo,无法实现 厂商/离线推送。真的贼坑


评论/回复