setTitle($title); $notify->setBody($body); //点击通知后续动作,目前支持以下后续动作: //1、intent:打开应用内特定页面url:打开网页地址。2、payload:自定义消息内容启动应用。3、payload_custom:自定义消息内容不启动应用。4、startapp:打开应用首页。5、none:纯通知,无后续动作 $notify->setClickType("startapp"); //保证有振动 $notify->setChannelId('channelId1'); $notify->setChannelName('channelName1'); $notify->setChannelLevel(4); $message = new GTPushMessage(); $message->setNotification($notify); //设置推送参数 $push = new GTPushRequest(); $time = time(); $rand = rand(1000, 9999); $rId = $time . $rand . $adminId; $push->setRequestId($rId); $push->setPushMessage($message); $push->setCid($cId); //创建API,APPID等配置参考 环境要求 进行获取 $api = new GTClient(self::domainUrl, self::appKey, self::appId, self::masterSecret); $api->pushApi()->pushToSingleByCid($push); } public static function nPush($adminId, $cId, $title, $body) { $set = new GTSettings(); $set->setTtl(3600000); $strategy = new GTStrategy(); $strategy->setDefault(GTStrategy::STRATEGY_THIRD_FIRST); // $strategy->setIos(GTStrategy::STRATEGY_GT_ONLY); // $strategy->setOp(GTStrategy::STRATEGY_THIRD_FIRST); // $strategy->setHw(GTStrategy::STRATEGY_THIRD_ONLY); $strategy->setHw(GTStrategy::STRATEGY_GT_FIRST); $set->setStrategy($strategy); //设置推送参数 $time = time(); $rand = rand(1000, 9999); $rId = $time . $rand . $adminId; $push = new GTPushRequest(); $push->setRequestId($rId); $push->setSettings($set); $message = new GTPushMessage(); $notify = new GTNotification(); $notify->setTitle($title); $notify->setBody($body); $notify->setChannelLevel(4); //点击通知后续动作,目前支持以下后续动作: //1、intent:打开应用内特定页面url:打开网页地址。2、payload:自定义消息内容启动应用。3、payload_custom:自定义消息内容不启动应用。4、startapp:打开应用首页。5、none:纯通知,无后续动作 $notify->setClickType("startapp"); $message->setNotification($notify); $push->setPushMessage($message); $push->setCid($cId); //厂商推送消息参数 $pushChannel = new GTPushChannel(); //ios $ios = new GTIos(); $ios->setType("notify"); $ios->setAutoBadge("1"); $ios->setPayload("ios_payload"); $ios->setApnsCollapseId("apnsCollapseId"); //aps设置 $aps = new GTAps(); $aps->setContentAvailable(0); $aps->setSound("com.gexin.ios.silenc"); $aps->setCategory("category"); $aps->setThreadId("threadId"); $alert = new GTAlert(); $alert->setTitle("alert title"); $alert->setBody("alert body"); $alert->setActionLocKey("ActionLocKey"); $alert->setLocKey("LocKey"); $alert->setLocArgs(array("LocArgs1","LocArgs2")); $alert->setLaunchImage("LaunchImage"); $alert->setTitleLocKey("TitleLocKey"); $alert->setTitleLocArgs(array("TitleLocArgs1","TitleLocArgs2")); $alert->setSubtitle("Subtitle"); $alert->setSubtitleLocKey("SubtitleLocKey"); $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($title); $thirdNotification->setBody("body".self::micro_time()); $thirdNotification->setClickType(GTThirdNotification::CLICK_TYPE_URL); $thirdNotification->setIntent("intent:#Intent;component=你的包名/你要打开的 activity 全路径;S.parm1=value1;S.parm2=value2;end"); $thirdNotification->setUrl("http://docs.getui.com/getui/server/rest_v2/push/"); $thirdNotification->setPayload("payload"); $thirdNotification->setNotifyId(456666); $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); //创建API,APPID等配置参考 环境要求 进行获取 $api = new GTClient(self::domainUrl, self::appKey, self::appId, self::masterSecret); //处理返回结果 $result = $api->pushApi()->pushToSingleByCid($push); Yii::info('push/single/cid: ' . json_encode($result)); } public static function micro_time() { list($usec, $sec) = explode(" ", microtime()); $time = ($sec . substr($usec, 2, 3)); return $time; } // 执行cid单推 public static function newPush($cId, $title, $body) { $bodyArr['title'] = $title; $bodyArr['body'] = $body; $bodyArr['click_type'] = 'startapp'; $token = Yii::$app->redis->executeCommand('GET', ['getui_token']); if (empty($token)) { $token = self::getToken(); if ($token == "") { Yii::error('generate getui token fail'); return; } Yii::$app->redis->executeCommand('SET', ['getui_token', $token, 'EX', 86400]); } $client = new Client([ 'base_uri' => self::baseUrl, 'timeout' => 5.0, ]); try { $response = $client->request('POST', self::baseUrl . '/push/single/cid', [ 'form_params' => [ 'request_id' => time(), 'settings' => ['ttl' => 86400000], 'audience' => [ 'cid' => $cId, ], 'push_message' => [ 'notification' => $bodyArr ], 'headers' => [ 'token' => $token ] ] ]); } catch (GuzzleException $e) { Yii::error('push/single/cid: ' . $e->getMessage()); } // 失败,则生重新获取 token ,再发送 } public static function getToken() { $i = 0; do{ $client = new Client([ 'base_uri' => self::baseUrl, 'timeout' => 5.0 ]); $response = $client->request('POST', self::baseUrl . '/auth', [ 'form_params' => [ "sign" => hash('sha256', self::appKey . time() . self::masterSecret), "timestamp" => round(microtime(true) * 1000), "appkey" => self::appKey ] ]); $data = json_decode($response, true); }while(!isset($data['data']['token']) && $i++ < 3); if ($i > 3) { return ""; } return $data['data']['token']; } }