最近做一个后台发送消息推送到app(android和ios)的功能,该功能采用的是个推接口,基于PHP的,我用TP5来实现这个推送流程。先看官方demo吧。可以先参考官方给到的例子来看。
由于该APP下载量比较大,考虑到要推送给所有下载app的用户,所以这里就不能用针对于单个用户进行推送了,必须针对应用群体进行推送。场景如下:
文档中给到的php例子是这个:
</span><span style="color: #800080">$appIdList</span>=<span style="color: #0000ff">array</span><span style="color: #000000">(APPID);
</span><span style="color: #800080">$phoneTypeList</span>=<span style="color: #0000ff">array</span>('ANDROID'<span style="color: #000000">);
</span><span style="color: #800080">$provinceList</span>=<span style="color: #0000ff">array</span>('浙江'<span style="color: #000000">);
</span><span style="color: #800080">$tagList</span>=<span style="color: #0000ff">array</span>('haha'<span style="color: #000000">);
</span><span style="color: #800080">$cdt</span> = <span style="color: #0000ff">new</span><span style="color: #000000"> AppConditions();
</span><span style="color: #800080">$cdt</span>->addCondition(AppConditions::PHONE_TYPE,<span style="color: #800080">$phoneTypeList</span><span style="color: #000000">);
</span><span style="color: #800080">$cdt</span>->addCondition(AppConditions::REGION,<span style="color: #800080">$provinceList</span><span style="color: #000000">);
</span><span style="color: #800080">$cdt</span>->addCondition(AppConditions::TAG,<span style="color: #800080">$tagList</span><span style="color: #000000">);
</span><span style="color: #800080">$message</span>->set_appIdList(<span style="color: #800080">$appIdList</span><span style="color: #000000">);
</span><span style="color: #800080">$message</span>->set_conditions(<span style="color: #800080">$cdt</span><span style="color: #000000">);
</span><span style="color: #800080">$rep</span> = <span style="color: #800080">$igt</span>->pushMessageToApp(<span style="color: #800080">$message</span><span style="color: #000000">);
</span><span style="color: #008080">var_dump</span>(<span style="color: #800080">$rep</span><span style="color: #000000">);
</span><span style="color: #0000ff">echo</span> ("<br><br>"<span style="color: #000000">);
}
<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">;
}
?>
然后看我自己实现流程:
当我在后台对某一条消息进行推送的时候 利用ajax传过来消息标题和内容,在控制器中用一个方法去接收:
</span><span style="color: #0000ff">if</span> (<span style="color: #800080">$res</span>['result'] == 'ok'<span style="color: #000000">) {
</span><span style="color: #0000ff">return</span> ['code' => '1','msg' => '推送成功!'<span style="color: #000000">];
} </span><span style="color: #0000ff">else</span><span style="color: #000000"> {
</span><span style="color: #0000ff">return</span> ['code' => '0','msg' => '推送失败!'<span style="color: #000000">];
}
}</span></pre>
看一下这个geTui类存放位置以及代码:
我把下载的sdk放在extend文件夹下了。
{
<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">//</span><span style="color: #008000">测试</span>
<span style="color: #0000ff">private</span> <span style="color: #800080">$appkey</span> = ''<span style="color: #000000">;
</span><span style="color: #0000ff">private</span> <span style="color: #800080">$appid</span> = ''<span style="color: #000000">;
</span><span style="color: #0000ff">private</span> <span style="color: #800080">$mastersecret</span> = ''<span style="color: #000000">;
</span><span style="color: #008000">//</span><span style="color: #008000">群推接口案例</span>
<span style="color: #0000ff">function</span> pushMessageToApp(<span style="color: #800080">$mes</span>,<span style="color: #800080">$listId</span><span style="color: #000000">){
import(</span>'getui.IGt','','.Push.<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>'<span style="color: #000000">);
</span><span style="color: #800080">$igt</span> = <span style="color: #0000ff">new</span> \IGeTui(<span style="color: #800080">$this</span>->host,<span style="color: #800080">$this</span>->appkey,<span style="color: #800080">$this</span>-><span style="color: #000000">mastersecret);
</span><span style="color: #800080">$template</span> = <span style="color: #800080">$this</span>->IGtTransmissionTemplateDemos(<span style="color: #800080">$mes</span>,<span style="color: #800080">$listId</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">$template = IGtLinkTemplateDemo();
//个推信息体
//基于应用消息体</span>
Loader::import('getui\igetui\IGT.AppMessage',<span style="color: #000000"> EXTEND_PATH);
</span><span style="color: #800080">$message</span> = <span style="color: #0000ff">new</span><span style="color: #000000"> \IGtAppMessage();
</span><span style="color: #800080">$message</span>->set_isOffline(<span style="color: #0000ff">true</span><span style="color: #000000">);
</span><span style="color: #800080">$message</span>->set_offlineExpireTime(10 * 60 * 1000);<span style="color: #008000">//</span><span style="color: #008000">离线时间单位为毫秒,例,两个小时离线为3600*1000*2</span>
<span style="color: #800080">$message</span>->set_data(<span style="color: #800080">$template</span><span style="color: #000000">);
</span><span style="color: #800080">$appIdList</span>=<span style="color: #0000ff">array</span>(<span style="color: #800080">$this</span> -><span style="color: #000000"> appid);
</span><span style="color: #800080">$phoneTypeList</span>=<span style="color: #0000ff">array</span>('ANDROID'<span style="color: #000000">);//忽略了
</span><span style="color: #800080">$provinceList</span>=<span style="color: #0000ff">array</span>('浙江'<span style="color: #000000">);//这个也忽略了
</span><span style="color: #800080">$tagList</span>=<span style="color: #0000ff">array</span>('haha'<span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">用户属性
//$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">$message</span>->set_appIdList(<span style="color: #800080">$appIdList</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">$message->set_conditions($cdt->getCondition());</span>
<span style="color: #800080">$rep</span> = <span style="color: #800080">$igt</span>->pushMessageToApp(<span style="color: #800080">$message</span><span style="color: #000000">);
</span><span style="color: #0000ff">return</span> <span style="color: #800080">$rep</span><span style="color: #000000">;
}
<span style="color: #008000">//<span style="color: #008000">所有推送接口均支持四个消息模板,依次为通知弹框下载模板,通知链接模板,通知透传模板,透传模板
//注:IOS离线推送需通过APN进行转发,需填写pushInfo字段,目前仅不支持通知弹框下载功能
<span style="color: #0000ff">function</span> IGtTransmissionTemplateDemos(<span style="color: #800080">$mes</span>,'.Push.<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>'<span style="color: #000000">);//引入sdk<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>
</span><span style="color: #800080">$template</span> = <span style="color: #0000ff">new</span><span style="color: #000000"> \IGtTransmissionTemplate();
</span><span style="color: #800080">$template</span>->set_appId(<span style="color: #800080">$this</span> -> appid);<span style="color: #008000">//</span><span style="color: #008000">应用appid</span>
<span style="color: #800080">$template</span>->set_appkey(<span style="color: #800080">$this</span>->appkey);<span style="color: #008000">//</span><span style="color: #008000">应用appkey</span>
<span style="color: #800080">$template</span>->set_transmissionType(2);<span style="color: #008000">//</span><span style="color: #008000">透传消息类型</span>
<span style="color: #800080">$template</span>->set_transmissionContent(json_encode(<span style="color: #800080">$listId</span>));<span style="color: #008000">//</span><span style="color: #008000">透传<a href="https://www.jb51.cc/tag/neirong/" target="_blank" class="keywords">内容</a>
//APN高级推送</span>
Loader::import('getui\igetui\IGT.APNPayload',<span style="color: #000000"> EXTEND_PATH);
</span><span style="color: #800080">$apn</span> = <span style="color: #0000ff">new</span><span style="color: #000000"> \IGtAPNPayload();
</span><span style="color: #800080">$alertmsg</span>=<span style="color: #0000ff">new</span><span style="color: #000000"> \DictionaryAlertMsg();
</span><span style="color: #800080">$alertmsg</span>->body=<span style="color: #800080">$mes</span>['content'<span style="color: #000000">];
</span><span style="color: #800080">$alertmsg</span>->actionLocKey="查看"<span style="color: #000000">;
</span><span style="color: #800080">$alertmsg</span>->locKey=<span style="color: #800080">$listId</span>['content'<span style="color: #000000">];
</span><span style="color: #800080">$alertmsg</span>->locArgs=<span style="color: #0000ff">array</span>("locargs"<span style="color: #000000">);
</span><span style="color: #800080">$alertmsg</span>->launchImage="launchimage"<span style="color: #000000">;
<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">$apn</span>->alertMsg=<span style="color: #800080">$alertmsg</span><span style="color: #000000">;
</span><span style="color: #800080">$apn</span>->badge=1<span style="color: #000000">;
</span><span style="color: #800080">$apn</span>->sound=""<span style="color: #000000">;
</span><span style="color: #800080">$apn</span>->add_customMsg("payload","payload"<span style="color: #000000">);
</span><span style="color: #800080">$apn</span>->contentAvailable=1<span style="color: #000000">;
</span><span style="color: #800080">$apn</span>->category="ACTIONABLE"<span style="color: #000000">;
</span><span style="color: #800080">$template</span>->set_apnInfo(<span style="color: #800080">$apn</span><span style="color: #000000">);
</span><span style="color: #008000">//</span><span style="color: #008000">PushApn老方式传参
// $template = new IGtAPNTemplate();
// $template->set_pushInfo("",10,"","com.gexin.ios.silence","");
<span style="color: #0000ff">return</span> <span style="color: #800080">$template</span><span style="color: #000000">;
}
}
好了,如果前端(android和ios)都以及做好接收处理的话,应该是没问题的了,该案例应用场景于所以下载app的客户,包含注册和未注册的,如果是针对于所有以及注册的用户进行推送的话 建议使用对单个用户进行推送消息。
内容,为了尊重他人劳动,转载请注明本文地址:
原文链接:https://www.f2er.com/thinkphp/71860.html