| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- namespace common\components;
- use GTClient;
- use GTNotification;
- use GTPushMessage;
- use GTPushRequest;
- 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)
- {
- //设置推送参数
- $time = time();
- $rand = rand(1000, 9999);
- $rId = $time . $rand . $adminId;
- $push = new GTPushRequest();
- $push->setRequestId($rId);
- $message = new GTPushMessage();
- $notify = new GTNotification();
- $notify->setTitle($title);
- $notify->setBody($body);
- //点击通知后续动作,目前支持以下后续动作:
- //1、intent:打开应用内特定页面url:打开网页地址。2、payload:自定义消息内容启动应用。3、payload_custom:自定义消息内容不启动应用。4、startapp:打开应用首页。5、none:纯通知,无后续动作
- $notify->setClickType("startapp");
- $message->setNotification($notify);
- $push->setPushMessage($message);
- $push->setCid($cId);
- //创建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));
- }
- // 执行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'];
- }
- }
|