Thinkphp5.0整合个推例子

前端之家收集整理的这篇文章主要介绍了Thinkphp5.0整合个推例子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

最近做一个后台发送消息推送到app(android和ios)的功能,该功能采用的是个推接口,基于PHP的,我用TP5来实现这个推送流程。先看官方demo吧。可以先参考官方给到的例子来看

由于该APP下载量比较大,考虑到要推送给所有下载app的用户,所以这里就不能用针对于单个用户进行推送了,必须针对应用群体进行推送。场景如下:

 文档中给到的php例子是这个:

("Content-Type: text/html; charset=utf-8"(() . '/' . 'IGt.Push.php'('APPKEY','请输入您的APPKEY'('APPID','请输入您的APPID'('MASTERSECRET','请输入您的MASTERSECRET'('HOST','http://sdk.open.api.igexin.com/apiex.htm'('CID','请输入您的CID' = IGeTui(HOST,APPKEY, = = ->set_isOffline(->set_offlineExpireTime(10 * 60 * 1000); ->set_data(</span><span style="color: #800080"&gt;$appIdList</span>=<span style="color: #0000ff"&gt;array</span><span style="color: #000000"&gt;(APPID); </span><span style="color: #800080"&gt;$phoneTypeList</span>=<span style="color: #0000ff"&gt;array</span>('ANDROID'<span style="color: #000000"&gt;); </span><span style="color: #800080"&gt;$provinceList</span>=<span style="color: #0000ff"&gt;array</span>('浙江'<span style="color: #000000"&gt;); </span><span style="color: #800080"&gt;$tagList</span>=<span style="color: #0000ff"&gt;array</span>('haha'<span style="color: #000000"&gt;); </span><span style="color: #800080"&gt;$cdt</span> = <span style="color: #0000ff"&gt;new</span><span style="color: #000000"&gt; AppConditions(); </span><span style="color: #800080"&gt;$cdt</span>->addCondition(AppConditions::PHONE_TYPE,<span style="color: #800080"&gt;$phoneTypeList</span><span style="color: #000000"&gt;); </span><span style="color: #800080"&gt;$cdt</span>->addCondition(AppConditions::REGION,<span style="color: #800080"&gt;$provinceList</span><span style="color: #000000"&gt;); </span><span style="color: #800080"&gt;$cdt</span>->addCondition(AppConditions::TAG,<span style="color: #800080"&gt;$tagList</span><span style="color: #000000"&gt;); </span><span style="color: #800080"&gt;$message</span>->set_appIdList(<span style="color: #800080"&gt;$appIdList</span><span style="color: #000000"&gt;); </span><span style="color: #800080"&gt;$message</span>->set_conditions(<span style="color: #800080"&gt;$cdt</span><span style="color: #000000"&gt;); </span><span style="color: #800080"&gt;$rep</span> = <span style="color: #800080"&gt;$igt</span>->pushMessageToApp(<span style="color: #800080"&gt;$message</span><span style="color: #000000"&gt;); </span><span style="color: #008080"&gt;var_dump</span>(<span style="color: #800080"&gt;$rep</span><span style="color: #000000"&gt;); </span><span style="color: #0000ff"&gt;echo</span> ("<br><br>"<span style="color: #000000"&gt;);

}

<span style="color: #0000ff">function<span style="color: #000000"> IGtLinkTemplateDemo(){
<span style="color: #800080">$template = <span style="color: #0000ff">new<span style="color: #000000"> IGtLinkTemplate();
<span style="color: #800080">$template ->set_appId(APPID);<span style="color: #008000">//<span style="color: #008000">应用appid
<span style="color: #800080">$template ->set_appkey(APPKEY);<span style="color: #008000">//<span style="color: #008000">应用appkey
<span style="color: #800080">$template ->set_title("请输入通知标题");<span style="color: #008000">//<span style="color: #008000">通知栏标题
<span style="color: #800080">$template ->set_text("请输入通知内容");<span style="color: #008000">//<span style="color: #008000">通知栏内容
<span style="color: #800080">$template ->set_logo("");<span style="color: #008000">//<span style="color: #008000">通知栏logo
<span style="color: #800080">$template ->set_isRing(<span style="color: #0000ff">true);<span style="color: #008000">//<span style="color: #008000">是否响铃
<span style="color: #800080">$template ->set_isVibrate(<span style="color: #0000ff">true);<span style="color: #008000">//<span style="color: #008000">是否震动
<span style="color: #800080">$template ->set_isClearable(<span style="color: #0000ff">true);<span style="color: #008000">//<span style="color: #008000">通知栏是否可清除
<span style="color: #800080">$template ->set_url("
http://www.getui.com/");<span style="color: #008000">//<span style="color: #008000">打开连接地址
//$template->set_notifyStyle(0);
//$template->set_duration(BEGINTIME,ENDTIME); //设置ANDROID客户端在此时间区间内展示消息
//iOS推送需要设置的pushInfo字段
// $apn = new IGtAPNPayload();
// $apn->alertMsg = "alertMsg";
// $apn->badge = 11;
// $apn->actionLocKey = "启动";
// // $apn->category = "ACTIONABLE";
// // $apn->contentAvailable = 1;
// $apn->locKey = "请输入通知栏内容";
// $apn->title = "请输入通知栏标题";
// $apn->titleLocArgs = array("titleLocArgs");
// $apn->titleLocKey = "请输入通知栏标题";
// $apn->body = "body";
// $apn->customMsg = array("payload"=>"payload");
// $apn->launchImage = "launchImage";
// $apn->locArgs = array("locArgs");
//
// $apn->sound=("test1.wav");;
// $template->set_apnInfo($apn);
<span style="color: #0000ff">return <span style="color: #800080">$template<span style="color: #000000">;
}
?>

然后下载sdk,下载地址:

然后看我自己实现流程:

当我在后台对某一条消息进行推送的时候 利用ajax传过来消息标题内容,在控制器中用一个方法去接收:

-> (->token == 2 '' = Request::instance()->param('list_id' = Request::instance()->param('list_title' = ='type' => 'list','value' => ,'title' => '消息标题','content' => ,//消息内容 = ->pushMessageToApp(调用推送类
    </span><span style="color: #0000ff"&gt;if</span> (<span style="color: #800080"&gt;$res</span>['result'] == 'ok'<span style="color: #000000"&gt;) {
        </span><span style="color: #0000ff"&gt;return</span> ['code' => '1','msg' => '推送成功!'<span style="color: #000000"&gt;];
    } </span><span style="color: #0000ff"&gt;else</span><span style="color: #000000"&gt; {
        </span><span style="color: #0000ff"&gt;return</span> ['code' => '0','msg' => '推送失败!'<span style="color: #000000"&gt;];
    }
}</span></pre>

看一下这个geTui类存放位置以及代码

我把下载的sdk放在extend文件夹下了。

然后重新创建了一个名为geTui的PHP文件

PHP namespace app\push; <span style="color: #0000ff">class<span style="color: #000000"> GeTui
{
<span style="color: #0000ff">private <span style="color: #800080">$host = 'http://sdk.open.api.igexin.com/apiex.htm'<span style="color: #000000">;

</span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;测试</span>
<span style="color: #0000ff"&gt;private</span> <span style="color: #800080"&gt;$appkey</span> = ''<span style="color: #000000"&gt;;
</span><span style="color: #0000ff"&gt;private</span> <span style="color: #800080"&gt;$appid</span> = ''<span style="color: #000000"&gt;;
</span><span style="color: #0000ff"&gt;private</span> <span style="color: #800080"&gt;$mastersecret</span> = ''<span style="color: #000000"&gt;;

</span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;群推接口案例</span>
<span style="color: #0000ff"&gt;function</span> pushMessageToApp(<span style="color: #800080"&gt;$mes</span>,<span style="color: #800080"&gt;$listId</span><span style="color: #000000"&gt;){
    import(</span>'getui.IGt','','.Push.<a href="/tag/PHP/" target="_blank" class="keywords">PHP</a>'<span style="color: #000000"&gt;);
    </span><span style="color: #800080"&gt;$igt</span> = <span style="color: #0000ff"&gt;new</span> \IGeTui(<span style="color: #800080"&gt;$this</span>->host,<span style="color: #800080"&gt;$this</span>->appkey,<span style="color: #800080"&gt;$this</span>-><span style="color: #000000"&gt;mastersecret);
    </span><span style="color: #800080"&gt;$template</span> = <span style="color: #800080"&gt;$this</span>->IGtTransmissionTemplateDemos(<span style="color: #800080"&gt;$mes</span>,<span style="color: #800080"&gt;$listId</span><span style="color: #000000"&gt;);
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;$template = IGtLinkTemplateDemo();
    //个推信息体
    //基于应用消息体</span>
    Loader::import('getui\igetui\IGT.AppMessage',<span style="color: #000000"&gt; EXTEND_PATH);
    </span><span style="color: #800080"&gt;$message</span> = <span style="color: #0000ff"&gt;new</span><span style="color: #000000"&gt; \IGtAppMessage();
    </span><span style="color: #800080"&gt;$message</span>->set_isOffline(<span style="color: #0000ff"&gt;true</span><span style="color: #000000"&gt;);
    </span><span style="color: #800080"&gt;$message</span>->set_offlineExpireTime(10 * 60 * 1000);<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;离线时间单位为毫秒,例,两个小时离线为3600*1000*2</span>
    <span style="color: #800080"&gt;$message</span>->set_data(<span style="color: #800080"&gt;$template</span><span style="color: #000000"&gt;);

    </span><span style="color: #800080"&gt;$appIdList</span>=<span style="color: #0000ff"&gt;array</span>(<span style="color: #800080"&gt;$this</span> -><span style="color: #000000"&gt; appid);
    </span><span style="color: #800080"&gt;$phoneTypeList</span>=<span style="color: #0000ff"&gt;array</span>('ANDROID'<span style="color: #000000"&gt;);//忽略了
    </span><span style="color: #800080"&gt;$provinceList</span>=<span style="color: #0000ff"&gt;array</span>('浙江'<span style="color: #000000"&gt;);//这个也忽略了
    </span><span style="color: #800080"&gt;$tagList</span>=<span style="color: #0000ff"&gt;array</span>('haha'<span style="color: #000000"&gt;);
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;用户属性
    //$age = array("0000","0010");


    //$cdt = new AppConditions();
    // $cdt->addCondition(AppConditions::PHONE_TYPE,$phoneTypeList);
    // $cdt->addCondition(AppConditions::REGION,$provinceList);
    //$cdt->addCondition(AppConditions::TAG,$tagList);
    //$cdt->addCondition("age",$age);</span>

    <span style="color: #800080"&gt;$message</span>->set_appIdList(<span style="color: #800080"&gt;$appIdList</span><span style="color: #000000"&gt;);
    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;$message->set_conditions($cdt->getCondition());</span>

    <span style="color: #800080"&gt;$rep</span> = <span style="color: #800080"&gt;$igt</span>->pushMessageToApp(<span style="color: #800080"&gt;$message</span><span style="color: #000000"&gt;);

    </span><span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$rep</span><span style="color: #000000"&gt;;
}

<span style="color: #008000">//<span style="color: #008000">所有推送接口均支持四个消息模板,依次为通知弹框下载模板,通知链接模板,通知透传模板,透传模板
//注:IOS离线推送需通过APN进行转发,需填写pushInfo字段,目前仅不支持通知弹框下载功能

<span style="color: #0000ff"&gt;function</span> IGtTransmissionTemplateDemos(<span style="color: #800080"&gt;$mes</span>,'.Push.<a href="/tag/PHP/" target="_blank" class="keywords">PHP</a>'<span style="color: #000000"&gt;);//引入sdk<a href="/tag/wenjian/" target="_blank" class="keywords">文件</a>
    </span><span style="color: #800080"&gt;$template</span> =  <span style="color: #0000ff"&gt;new</span><span style="color: #000000"&gt; \IGtTransmissionTemplate();
    </span><span style="color: #800080"&gt;$template</span>->set_appId(<span style="color: #800080"&gt;$this</span> -> appid);<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;应用appid</span>
    <span style="color: #800080"&gt;$template</span>->set_appkey(<span style="color: #800080"&gt;$this</span>->appkey);<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;应用appkey</span>
    <span style="color: #800080"&gt;$template</span>->set_transmissionType(2);<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;透传消息类型</span>
    <span style="color: #800080"&gt;$template</span>->set_transmissionContent(json_encode(<span style="color: #800080"&gt;$listId</span>));<span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;透传<a href="/tag/neirong/" target="_blank" class="keywords">内容</a>

    //APN高级推送</span>
    Loader::import('getui\igetui\IGT.APNPayload',<span style="color: #000000"&gt; EXTEND_PATH);
    </span><span style="color: #800080"&gt;$apn</span> = <span style="color: #0000ff"&gt;new</span><span style="color: #000000"&gt; \IGtAPNPayload();
    </span><span style="color: #800080"&gt;$alertmsg</span>=<span style="color: #0000ff"&gt;new</span><span style="color: #000000"&gt; \DictionaryAlertMsg();
    </span><span style="color: #800080"&gt;$alertmsg</span>->body=<span style="color: #800080"&gt;$mes</span>['content'<span style="color: #000000"&gt;];
    </span><span style="color: #800080"&gt;$alertmsg</span>->actionLocKey="查看"<span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$alertmsg</span>->locKey=<span style="color: #800080"&gt;$listId</span>['content'<span style="color: #000000"&gt;];
    </span><span style="color: #800080"&gt;$alertmsg</span>->locArgs=<span style="color: #0000ff"&gt;array</span>("locargs"<span style="color: #000000"&gt;);
    </span><span style="color: #800080"&gt;$alertmsg</span>->launchImage="launchimage"<span style="color: #000000"&gt;;

<span style="color: #008000">//<span style="color: #008000"> IOS8.2 支持
<span style="color: #800080">$alertmsg->title=<span style="color: #800080">$mes['title'<span style="color: #000000">];
<span style="color: #800080">$alertmsg->titleLocKey="电力头条"<span style="color: #000000">;
<span style="color: #800080">$alertmsg->titleLocArgs=<span style="color: #0000ff">array("TitleLocArg"<span style="color: #000000">);

    </span><span style="color: #800080"&gt;$apn</span>->alertMsg=<span style="color: #800080"&gt;$alertmsg</span><span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$apn</span>->badge=1<span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$apn</span>->sound=""<span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$apn</span>->add_customMsg("payload","payload"<span style="color: #000000"&gt;);
    </span><span style="color: #800080"&gt;$apn</span>->contentAvailable=1<span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$apn</span>->category="ACTIONABLE"<span style="color: #000000"&gt;;
    </span><span style="color: #800080"&gt;$template</span>->set_apnInfo(<span style="color: #800080"&gt;$apn</span><span style="color: #000000"&gt;);

    </span><span style="color: #008000"&gt;//</span><span style="color: #008000"&gt;PushApn老方式传参

// $template = new IGtAPNTemplate();
// $template->set_pushInfo("",10,"","com.gexin.ios.silence","");

    <span style="color: #0000ff"&gt;return</span> <span style="color: #800080"&gt;$template</span><span style="color: #000000"&gt;;
}

}

好了,如果前端(android和ios)都以及做好接收处理的话,应该是没问题的了,该案例应用场景于所以下载app的客户,包含注册和未注册的,如果是针对于所有以及注册用户进行推送的话 建议使用对单个用户进行推送消息。

内容,为了尊重他人劳动,转载请注明本文地址:

猜你在找的ThinkPHP相关文章