Thinkphp5.0整合个推例子

时间:2024-01-20 11:56:15

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

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

Thinkphp5.0整合个推例子

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

<?php
//消息推送Demo
header("Content-Type: text/html; charset=utf-8");
require_once(dirname(__FILE__) . '/' . 'IGt.Push.php');
define('APPKEY','请输入您的APPKEY');
define('APPID','请输入您的APPID');
define('MASTERSECRET','请输入您的MASTERSECRET');
define('HOST','http://sdk.open.api.igexin.com/apiex.htm');
define('CID','请输入您的CID');
pushMessageToApp();
function pushMessageToApp(){
$igt = new IGeTui(HOST,APPKEY,MASTERSECRET);
$template = IGtLinkTemplateDemo();
//个推信息体
//基于应用消息体
$message = new IGtAppMessage();
$message->set_isOffline(true);
$message->set_offlineExpireTime(10 * 60 * 1000);//离线时间单位为毫秒,例,两个小时离线为3600*1000*2
$message->set_data($template); $appIdList=array(APPID);
$phoneTypeList=array('ANDROID');
$provinceList=array('浙江');
$tagList=array('haha'); $cdt = new AppConditions();
$cdt->addCondition(AppConditions::PHONE_TYPE, $phoneTypeList);
$cdt->addCondition(AppConditions::REGION, $provinceList);
$cdt->addCondition(AppConditions::TAG, $tagList); $message->set_appIdList($appIdList);
$message->set_conditions($cdt); $rep = $igt->pushMessageToApp($message); var_dump($rep);
echo ("<br><br>");
} function IGtLinkTemplateDemo(){
$template = new IGtLinkTemplate();
$template ->set_appId(APPID);//应用appid
$template ->set_appkey(APPKEY);//应用appkey
$template ->set_title("请输入通知标题");//通知栏标题
$template ->set_text("请输入通知内容");//通知栏内容
$template ->set_logo("");//通知栏logo
$template ->set_isRing(true);//是否响铃
$template ->set_isVibrate(true);//是否震动
$template ->set_isClearable(true);//通知栏是否可清除
$template ->set_url("http://www.getui.com/");//打开连接地址
//$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);
return $template;
}
?>

然后下载sdk,下载地址:http://docs.getui.com/download.html

然后看我自己实现流程:

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

//手机推送
public function pushPhone()
{
$this->base();
if ($this->token == 2) {
return '';
}
$list_id = Request::instance()->param('list_id');
$list_title = Request::instance()->param('list_title');
$info = new geTui();//实例化个推类
$listId = [
'type' => 'list',
'value' => $list_id,
'title' => '消息标题',
'content' => $list_title,//消息内容
];
$res = $info->pushMessageToApp($listId);/调用推送类 if ($res['result'] == 'ok') {
return ['code' => '1', 'msg' => '推送成功!'];
} else {
return ['code' => '0', 'msg' => '推送失败!'];
}
}

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

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

Thinkphp5.0整合个推例子

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

<?php
namespace app\push;
use think\Loader; class GeTui
{
private $host = 'http://sdk.open.api.igexin.com/apiex.htm'; //测试
private $appkey = '';
private $appid = '';
private $mastersecret = ''; //群推接口案例
function pushMessageToApp($mes,$listId){
import('getui.IGt', '', '.Push.php');
$igt = new \IGeTui($this->host, $this->appkey, $this->mastersecret);
$template = $this->IGtTransmissionTemplateDemos($mes,$listId);
//$template = IGtLinkTemplateDemo();
//个推信息体
//基于应用消息体
Loader::import('getui\igetui\IGT.AppMessage', EXTEND_PATH);
$message = new \IGtAppMessage();
$message->set_isOffline(true);
$message->set_offlineExpireTime(10 * 60 * 1000);//离线时间单位为毫秒,例,两个小时离线为3600*1000*2
$message->set_data($template); $appIdList=array($this -> appid);
$phoneTypeList=array('ANDROID');//忽略了
$provinceList=array('浙江');//这个也忽略了
$tagList=array('haha');
//用户属性
//$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); $message->set_appIdList($appIdList);
//$message->set_conditions($cdt->getCondition()); $rep = $igt->pushMessageToApp($message); return $rep;
} //所有推送接口均支持四个消息模板,依次为通知弹框下载模板,通知链接模板,通知透传模板,透传模板
//注:IOS离线推送需通过APN进行转发,需填写pushInfo字段,目前仅不支持通知弹框下载功能 function IGtTransmissionTemplateDemos($mes,$listId){
import('getui.IGt', '', '.Push.php');//引入sdk文件
$template = new \IGtTransmissionTemplate();
$template->set_appId($this -> appid);//应用appid
$template->set_appkey($this->appkey);//应用appkey
$template->set_transmissionType(2);//透传消息类型
$template->set_transmissionContent(json_encode($listId));//透传内容 //APN高级推送
Loader::import('getui\igetui\IGT.APNPayload', EXTEND_PATH);
$apn = new \IGtAPNPayload();
$alertmsg=new \DictionaryAlertMsg();
$alertmsg->body=$mes['content'];
$alertmsg->actionLocKey="查看";
$alertmsg->locKey=$listId['content'];
$alertmsg->locArgs=array("locargs");
$alertmsg->launchImage="launchimage";
// IOS8.2 支持
$alertmsg->title=$mes['title'];
$alertmsg->titleLocKey="电力头条";
$alertmsg->titleLocArgs=array("TitleLocArg"); $apn->alertMsg=$alertmsg;
$apn->badge=1;
$apn->sound="";
$apn->add_customMsg("payload","payload");
$apn->contentAvailable=1;
$apn->category="ACTIONABLE";
$template->set_apnInfo($apn); //PushApn老方式传参
// $template = new IGtAPNTemplate();
// $template->set_pushInfo("", 10, "", "com.gexin.ios.silence", "", "", "", ""); return $template;
} }

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

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

http://www.cnblogs.com/luokakale/p/9047032.html