1,安装项目依赖。
composer require getuilaboratory/getui-pushapi-php-client-v2
2,根据用户Cid单推
public function pushToSingleByCid($system = 'android'){
//创建API,APPID等配置参考 环境要求 进行获取
$api = new \GTClient("https://restapi.getui.com","你的个推appkey", "你的个推appId","你的个推masterSecret");
//设置推送参数
$push = new \GTPushRequest();
$push->setRequestId($this->micro_time());
$message = new \GTPushMessage();
$notify = new \GTNotification();
$settings = new \GTSettings();
$strategy = new \GTStrategy();
$strategy->setDefault(\GTStrategy::STRATEGY_GT_FIRST);
$settings->setStrategy($strategy);
$notify->setTitle("订单提醒");
$notify->setBody("您有新的订单,请及时处理");
//点击通知后续动作,目前支持以下后续动作:
//1、intent:打开应用内特定页面url:打开网页地址。2、payload:自定义消息内容启动应用。3、payload_custom:自定义消息内容不启动应用。4、startapp:打开应用首页。5、none:纯通知,无后续动作
// $notify->setClickType("intent:#Intent;launchFlags=0x04000000;action=android.intent.action.oppopush;component=and.dcloud.Lapp/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.title=测试标题;S.content=测试内容;S.payload=test;end");
$notify->setClickType("startapp");
//判断一下操作系统
if($system == "android"){
$message->setNotification($notify); //android
}elseif($system == "ios"){
$message->setTransmission("您有新的订单,请及时处理"); //ios
}
$push->setPushMessage($message);
$push->setSettings($settings);
$pushChannel = new \GTPushChannel();
//ios
$ios = new \GTIos();
$ios->setType("notify");
$ios->setAutoBadge("1");
$ios->setPayload("ios_payload");
$ios->setApnsCollapseId($this->micro_time());
//aps设置
$aps = new \GTAps();
$aps->setContentAvailable(0);
$aps->setSound("music");
$aps->setCategory("category");
$aps->setThreadId("threadId");
$alert = new \GTAlert();
$alert->setTitle("订单提醒");
$alert->setBody("您有新的订单,请及时处理");
$alert->setActionLocKey("ActionLocKey");
$alert->setLocKey("LocKey");
$alert->setLocArgs(array("LocArgs1","LocArgs2"));
$alert->setLaunchImage("LaunchImage");
$alert->setTitleLocKey("TitleLocKey");
$alert->setTitleLocArgs(array("TitleLocArgs1","TitleLocArgs2"));
$alert->setSubtitle("");
$alert->setSubtitleLocKey("");
$alert->setSubtitleLocArgs(array("subtitleLocArgs1","subtitleLocArgs2"));
$aps->setAlert($alert);
$ios->setAps($aps);
$multimedia = new \GTMultimedia();
$multimedia->setUrl("url");
$multimedia->setType(1);
$multimedia->setOnlyWifi(false);
$multimedia2 = new \GTMultimedia();
$multimedia2->setUrl("url2");
$multimedia2->setType(2);
$multimedia2->setOnlyWifi(true);
$ios->setMultimedia(array($multimedia));
$ios->addMultimedia($multimedia2);
$pushChannel->setIos($ios);
//安卓
$android = new \GTAndroid();
$ups = new \GTUps();
// $ups->setTransmission("ups Transmission");
$thirdNotification = new \GTThirdNotification();
$thirdNotification->setTitle("订单提醒");
$thirdNotification->setBody("您有新的订单,请及时处理");
$thirdNotification->setClickType(\GTThirdNotification::CLICK_TYPE_STAERAPP);
$thirdNotification->setIntent("intent:#Intent;component=and.dcloud.Lapp/io.dcloud.PandoraEntry;S.parm1=value1;S.parm2=value2;end");
$thirdNotification->setUrl("https://www.baidu.com");
$thirdNotification->setPayload("payload");
$thirdNotification->setNotifyId(mt_rand(0,2147483647));
$ups->addOption("HW","badgeAddNum",1);
$ups->addOption("OP","channel","Default");
$ups->addOption("OP","aaa","bbb");
$ups->addOption(null,"a","b");
$ups->setNotification($thirdNotification);
$android->setUps($ups);
$pushChannel->setAndroid($android);
$push->setPushChannel($pushChannel);
$push->setCid("用户cid");
$result = $api->pushApi()->pushToSingleByCid($push);
}
public function micro_time()
{
list($usec, $sec) = explode(" ", microtime());
$time = ($sec . substr($usec, 2, 3));
return $time;
}