用户风哥 發表於 2020-6-7 16:50:00

thinkphp5 对接手机uni-app的unipush推送(个推)

<p>    PHP对接unipush(推送)也是要按照个推的文档进行对接(其实unipush用的服务就是个推的,前者应该是和个推谈了合作,个推的厂商推送服务免费开放给uni的开发人员)</p>
<p>    先去个推官网下SDK:http://docs.getui.com/download.html</p>
<p>    个推文档:http://docs.getui.com/getui/server/php/push/</p>
<p>    </p>
<p>放入项目的extend文件夹(第三方扩展),并在getui的demo文件同级创建一个GeTui.php文件,用来实例化并调用,class代码贴在图片后面</p>
<p>  <img src="https://img2020.cnblogs.com/blog/1666005/202006/1666005-20200607162457791-1856951956.png"></p>
<p>&nbsp;</p>
<p>因为需求问题,我项目只有用到单用户的推送,所以这边只贴我测试并通过的代码,没有多推的示例</p>
<p>这里需要注意的是:<span style="font-size: 18px"><strong>消息内容决定了系统的通知栏的通知等级,如果通知栏一直没有消息,可以去查一下各自手机厂商的消息等级,对应修改文字</strong></span></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">&lt;?</span><span style="color: rgba(255, 0, 255, 1)">php

class GeTui
{
    private $host = 'http://sdk.open.api.igexin.com/apiex.htm';
    //测试
    private $appkey = '';
    private $appid = '';
    private $mastersecret = '';



    private function init($appid=null,$appkey=null,$mastersecret=null)
    {
      $this-&gt;appid = !empty($appid) ? $appid : "fdVf****aUq4";
      $this-&gt;appkey = !empty($appkey) ? $appkey : "fdVf****RUq4";
      $this-&gt;mastersecret = !empty($mastersecret) ? $mastersecret : "39pj****VtQ5";
      $this-&gt;host = "http://sdk.open.api.igexin.com/apiex.htm";
    }

    public function __construct($appid=null,$appkey=null,$mastersecret=null)
    {
      $this-&gt;init($appid,$appkey,$mastersecret);
      $this-&gt;__loader();
    }

    private function __loader()
    {
      require_once(dirname(__FILE__) . '/' . 'IGt.Push.php');
      require_once(dirname(__FILE__) . '/' . 'igetui/IGt.AppMessage.php');
      require_once(dirname(__FILE__) . '/' . 'igetui/IGt.TagMessage.php');
      require_once(dirname(__FILE__) . '/' . 'igetui/IGt.APNPayload.php');
      require_once(dirname(__FILE__) . '/' . 'igetui/template/IGt.BaseTemplate.php');
      require_once(dirname(__FILE__) . '/' . 'IGt.Batch.php');
      require_once(dirname(__FILE__) . '/' . 'igetui/utils/AppConditions.php');
      require_once(dirname(__FILE__) . '/' . 'igetui/template/notify/IGt.Notify.php');
      require_once(dirname(__FILE__) . '/' . 'igetui/IGt.MultiMedia.php');
      require_once(dirname(__FILE__) . '/' . 'payload/VOIPPayload.php');
    }

    //单推
    function pushMessageToSingle($cid){
      $igt = new \IGeTui($this-&gt;host,$this-&gt;appkey,$this-&gt;mastersecret);

      // 透传消息模板
      $template = $this-&gt;IGtTransmissionTemplateDemo();

      //定义"SingleMessage"
      $message = new \IGtSingleMessage();

      $message-&gt;set_isOffline(false);//是否离线
      $message-&gt;set_offlineExpireTime(3600*12*1000);//离线时间
      $message-&gt;set_data($template);//设置推送消息类型
      $message-&gt;set_PushNetWorkType(0);//设置是否根据WIFI推送消息,2为4G/3G/2G,1为wifi推送,0为不限制推送

      //接收方
      $target = new \IGtTarget();
      $target-&gt;set_appId($this-&gt;appid);
      $target-&gt;set_clientId($cid);

      try {
            $rep = $igt-&gt;pushMessageToSingle($message, $target);
            var_dump($rep);
            echo ("&lt;br&gt;&lt;br&gt;");

      }catch(RequestException $e){
            $requstId =e.getRequestId();
            //失败时重发
            $rep = $igt-&gt;pushMessageToSingle($message, $target,$requstId);
            var_dump($rep);
            echo ("&lt;br&gt;&lt;br&gt;");
      }
    }

    //透传消息模板
    public function IGtTransmissionTemplateDemo(){
      $listId = [
            'title' =&gt; '通知',
            'content' =&gt; '你有一条新消息',
            'payload' =&gt; [
                "push"=&gt; "inner",
                "event"=&gt; "warning",
                "silent"=&gt; false,
            ]
      ];
      $mes = [
            'title' =&gt; '通知',
            'content' =&gt; '你有一条新消息',
            'payload' =&gt; [
                "push"=&gt; "inner",   
                "event"=&gt; "warning",
                "silent"=&gt; false,
                "data"=&gt; ""
            ]
      ];
      $template =new \IGtTransmissionTemplate();

      $template-&gt;set_appId($this -&gt; appid);//应用appid
      $template-&gt;set_appkey($this-&gt;appkey);//应用appkey
      $template-&gt;set_transmissionType(1);//透传消息类型
      $template-&gt;set_transmissionContent(json_encode($listId));//透传内容

      //注意:如果设备离线(安卓),一定要设置厂商推送,不然接收不到推送(比如华为、小米等等)
      //S.title=的值为推送消息标题,对应5+ API中PushMessage对象的title属性值;
      //S.content=的值为推送消息内容,对应5+ API中PushMessage对象的content属性值;
      //S.payload=的值为推送消息的数据,对应5+ API中PushMessage对象的payload属性值;
      $intent = 'intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component=此处为打包APP的包名/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=标题;S.content=内容;S.payload=数据;end';

      $notify = new \IGtNotify();
      $notify-&gt;set_title('通知');
      $notify-&gt;set_content('你有一条新消息');
      $notify-&gt;set_intent($intent);
      $notify-&gt;set_type(NotifyInfo_type::_intent);
      $template-&gt;set3rdNotifyInfo($notify);

      //下面这些是苹果需要设置的,只要是ios系统的,都要设置这个,不然离线收不到
      //APN高级推送
      $alertmsg=new \DictionaryAlertMsg();
      $alertmsg-&gt;body=$mes['content'];
      $alertmsg-&gt;actionLocKey="查看";
      $alertmsg-&gt;locKey=$listId['content'];
      $alertmsg-&gt;locArgs=array("locargs");
      $alertmsg-&gt;launchImage="launchimage";
//      IOS8.2 支持
      $alertmsg-&gt;title=$mes['title'];
      $alertmsg-&gt;titleLocKey="测试";
      $alertmsg-&gt;titleLocArgs=array("TitleLocArg");

      $apn = new \IGtAPNPayload();
      $apn-&gt;alertMsg=$alertmsg;
      $apn-&gt;badge=0;
      $apn-&gt;sound="";
      $apn-&gt;add_customMsg("payload","payload");
      $apn-&gt;contentAvailable=0;
      $apn-&gt;category="ACTIONABLE";
      $template-&gt;set_apnInfo($apn);

      return $template;
    }


}</span></pre>
</div>
<p>&nbsp;</p>
<p>调用</p>
<div class="cnblogs_code">
<pre>    <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">个推消息推送</span>
    <span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">function</span> pushChat(<span style="color: rgba(128, 0, 128, 1)">$cid</span>=<span style="color: rgba(0, 0, 255, 1)">null</span>, <span style="color: rgba(128, 0, 128, 1)">$type</span>=<span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">)
    {
      </span><span style="color: rgba(0, 0, 255, 1)">if</span>(<span style="color: rgba(0, 0, 255, 1)">empty</span>(<span style="color: rgba(128, 0, 128, 1)">$cid</span><span style="color: rgba(0, 0, 0, 1)">)){
            </span><span style="color: rgba(0, 0, 255, 1)">die</span><span style="color: rgba(0, 0, 0, 1)">;
      }

      import(</span>'getui.GeTui', EXTEND_PATH,".php"<span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(128, 0, 128, 1)">$getui</span> = <span style="color: rgba(0, 0, 255, 1)">new</span> \GeTui(<span style="color: rgba(128, 0, 128, 1)">$appid</span>,<span style="color: rgba(128, 0, 128, 1)">$appkey</span>,<span style="color: rgba(128, 0, 128, 1)">$mastersecret</span><span style="color: rgba(0, 0, 0, 1)">);
      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">单发测试 $cid 客户端id 前端获取</span>
      <span style="color: rgba(128, 0, 128, 1)">$getui</span>-&gt;pushMessageToSingle(<span style="color: rgba(128, 0, 128, 1)">$cid</span><span style="color: rgba(0, 0, 0, 1)">);

    }</span></pre>
</div>
<p>&nbsp;</p><br><br>
来源:https://www.cnblogs.com/j-jian/p/13061118.html
頁: [1]
查看完整版本: thinkphp5 对接手机uni-app的unipush推送(个推)