首先,弄清楚概念
透传:只用把服务器使用API发送的数据推到个推服务器,不管客户端的情况(手机黑屏,或者app退出后台),这时候客户端可以拿到数据并且自己处理.
分为用户有感知透传(通知栏)和无感知透传(微信朋友圈中经常出现的小红点)
通知:直接把用户的消息在通知栏展示出来,客户端无法进行相应的处理操作
APN:我理解的就是苹果手机都是用这种传输的
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/PayloadKeyReference.html
使用app推送的6中情况
1.手机黑屏,app不在后台运行
2.手机黑屏,app在后台运行
3.手机不黑屏,app不在后台运行
4.手机不黑屏,app在后台运行
5.手机黑屏,app正在运行(打开界面)
6.手机不黑屏,app正在运行(打开当前app的界面)
除了第6种使用的是set_transmissionContent,其他的都使用了 apn
代码
composer下载个推的文件夹放到如下文件夹下面
在控制器中创建如下代码(支持安卓和IOS)
<?php
namespace Service\Controller;
use Base\Webbase;
class GetuiController extends Webbase {
private $host;
private $appkey;
private $appid;
private $mastersecret;
public function __construct()
{
//http的域名
$this->host = 'http://sdk.open.api.igexin.com/apiex.htm';
//定义常量, appId、appKey、masterSecret 采用本文档 "第二步 获取访问凭证 "中获得的应用配置
$this->appkey = '你自己的appkey ';
$this->appid = '你自己的appid ';
$this->mastersecret = '你自己的mastersecret ';
}
//群推接口案例
public function pushMessageToApp($users,$listId){
vendor("getuilaboratory.getui-pushapi-php-client.IGt#Push");
$igt = new \IGeTui($this->host,$this->appkey,$this->mastersecret);
//消息模版:
$template = $this->IGtNotificationTemplateDemo($listId);
// IOS8.2 支持
$apn = new \IGtAPNPayload();
$alertmsg=new \DictionaryAlertMsg();
$alertmsg->body= $listId['msg'];
$alertmsg->actionLocKey="actionLocKey";
$alertmsg->locKey='locKey';
$alertmsg->locArgs=array("locargs");
$alertmsg->launchImage="launchimage";
$alertmsg->title=$listId['title'];
$alertmsg->titleLocKey="titleLocKey";
$alertmsg->titleLocArgs=array("TitleLocArg");
$apn->alertMsg=$alertmsg;
$apn->badge=1;
$apn->add_customMsg("payload",$listId['id']);
$apn->contentAvailable=1; //推送直接带有透传数据
$apn->category="ACTIONABLE";
$template->set_apnInfo($apn);
//定义"SingleMessage"
// $message = new \IGtSingleMessage(); //单个用户推送
$message = new \IGtListMessage(); //多个用户推送
$message->set_isOffline(true);//是否离线
$message->set_offlineExpireTime(3600*12*1000);//离线时间
$message->set_data($template);//设置推送消息类型
$contentId = $igt->getContentId($message);
// //接收方
foreach ($users as $Alias){
$target = new \IGtTarget();
$target->set_appId($this->appid);
$target->set_alias($Alias);
$targetList[] = $target;
}
try {
//$rep = $igt->pushMessageToSingle($message, $target);
$rep = $igt->pushMessageToList($contentId, $targetList);
var_dump($rep);
echo ("<br><br>");
}catch(RequestException $e){
$requstId = $e.getRequestId();
//失败时重发
$rep = $igt->pushMessageToSingle($message, $target,$requstId);
var_dump($rep);
echo ("<br><br>");
}
}
public function IGtNotificationTemplateDemo($listId){
// $template = new \IGtNotificationTemplate();
$template = new \IGtTransmissionTemplate(); //**4个模板的类型一定要弄清楚** 上面的个推会发送两条
$template->set_appId($this->appid); //应用appid
$template->set_appkey($this->appkey); //应用appkey
$template->set_transmissionType(1); //透传消息类型
$listId = json_encode($listId,JSON_UNESCAPED_UNICODE);
$template->set_transmissionContent($listId);//透传内容
return $template;
}