| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- <?php
- namespace common\components;
- use GTAlert;
- use GTAndroid;
- use GTAps;
- use GTClient;
- use GTIos;
- use GTMultimedia;
- use GTNotification;
- use GTPushChannel;
- use GTPushMessage;
- use GTPushRequest;
- use GTSettings;
- use GTStrategy;
- use GTThirdNotification;
- use GTUps;
- use GuzzleHttp\Client;
- use GuzzleHttp\Exception\GuzzleException;
- use Yii;
- class pushNotice
- {
- const domainUrl = 'https://restapi.getui.com';
- const appKey = 'uC5ZdrWSV48cLRBJWMSId9';
- const appId = 'YjALucIHmI7DylcjlMZEx2';
- const masterSecret = 'zSbdBLOQrxAa5IcDNqIne7';
- //版本2:个推开放平台接口前缀(BaseUrl): https://restapi.getui.com/v2/$appId
- const baseUrl = 'https://restapi.getui.com/v2';
- public static function push($adminId, $cId, $title, $body)
- {
- $notify = new GTNotification();
- $notify->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(self::micro_time());
- $push->setSettings($set);
- $message = new GTPushMessage();
- $notify = new GTNotification();
- $notify->setTitle($title);
- $notify->setBody($body);
- $notify->setChannelId('channelId1');
- $notify->setChannelName('channelName1');
- $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($title);
- $alert->setBody($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);
- // ------------- ios end ---------------
- // ----------------- 安卓 -----------------
- $android = new GTAndroid();
- $ups = new GTUps();
- //$ups->setTransmission("ups Transmission");
- $ups->addOption("HW","badgeAddNum",1);
- //$ups->addOption("OP","channel","Default");
- //$ups->addOption("OP","aaa","bbb");
- //$ups->addOption(null,"a","b");
- $thirdNotification = new GTThirdNotification();
- $thirdNotification->setTitle($title);
- $thirdNotification->setBody("body-".self::micro_time().': ' . $body);
- $thirdNotification->setClickType(GTThirdNotification::CLICK_TYPE_STAERAPP);
- //$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(self::micro_time());
- $ups->setNotification($thirdNotification);
- $android->setUps($ups);
- $pushChannel->setAndroid($android);
- // ----------------- 安卓 end -----------------
- $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'];
- }
- }
|