Browse Source

对接个推

lizhen_gitee 6 months ago
parent
commit
277a9f6720
2 changed files with 277 additions and 0 deletions
  1. 53 0
      application/api/controller/Demo.php
  2. 224 0
      application/common/library/Getui.php

+ 53 - 0
application/api/controller/Demo.php

@@ -5,6 +5,12 @@ namespace app\api\controller;
 use app\common\controller\Api;
 use Aws\Sns\SnsClient;
 use Aws\Exception\AwsException;
+use app\common\library\Getui;
+
+use getui\GTClient;
+use getui\GTPushRequest;
+use getui\GTPushMessage;
+use getui\GTNotification;
 /**
  * 示例接口
  */
@@ -40,6 +46,53 @@ class Demo extends Api
         })
      */
 
+    public function getui2(){
+        $config = config('getui');
+        $this->appId        = $config['gt_app_id'];
+        $this->appKey       = $config['gt_app_key'];
+        $this->appSecret    = $config['gt_app_secret'];
+        $this->masterSecret = $config['gt_master_secret'];
+
+        //创建API,APPID等配置参考 环境要求 进行获取
+        $api = new GTClient("https://restapi.getui.com",$config['gt_app_key'], $config['gt_app_id'],$config['gt_master_secret']);
+        //设置推送参数
+        $push = new GTPushRequest();
+        $push->setRequestId("请求唯一标识号");
+        $message = new GTPushMessage();
+        $notify = new GTNotification();
+        $notify->setTitle("设置通知标题");
+        $notify->setBody("设置通知内容");
+        //点击通知后续动作,目前支持以下后续动作:
+        //1、intent:打开应用内特定页面url:打开网页地址。2、payload:自定义消息内容启动应用。3、payload_custom:自定义消息内容不启动应用。4、startapp:打开应用首页。5、none:纯通知,无后续动作
+        $notify->setClickType("none");
+        $message->setNotification($notify);
+        $push->setPushMessage($message);
+        $push->setCid("CID");
+        //处理返回结果
+        $result = $api->pushApi()->pushToSingleByCid($push);
+    }
+
+    public function getui(){
+        $getui = new Getui();
+        $cid = 1;
+        $title = '标题';
+        $body = '内容';
+        $ring_name = 'ringname';
+        $type = 1;
+        $platform = 'android';
+        $getui->push($cid,  $title,  $body, $ring_name, $type,  $platform);
+
+    }
+    public function createmsg(){
+        $getui = new Getui();
+
+        $title = '标题';
+        $body = '内容';
+        $type = 1;
+        $rs = $getui->sendtoall($title,  $body,  $type);
+
+    }
+
     public function test2(){
         $a = time();
         $b = $this->date_lang($a);

+ 224 - 0
application/common/library/Getui.php

@@ -0,0 +1,224 @@
+<?php
+
+namespace app\common\library;
+
+
+class Getui
+{
+    protected  $base_uri = "https://restapi.getui.com/v2/";
+    private    $appId;
+    private    $appKey;
+    private    $appSecret;
+    private    $masterSecret;
+
+    public function __construct()
+    {
+        $config = config('getui');
+        $this->appId        = $config['gt_app_id'];
+        $this->appKey       = $config['gt_app_key'];
+        $this->appSecret    = $config['gt_app_secret'];
+        $this->masterSecret = $config['gt_master_secret'];
+    }
+
+    //创建消息
+    public function sendtoall($title,  $body, $type = 1){
+        if (!$access_token = $this->auth()) {
+            return $this->error('ge tui token error');
+        }
+
+        // 通知
+        $notification = [
+            "notification"=> [
+                "title"=> $title,
+                "body"=> $body,
+                "click_type"=> "url",
+                "url"=> "https//:xxx"
+            ],
+        ];
+
+        // 透传
+        $transmission = [
+            'transmission' => json_encode([
+                "title"=> $title,
+                "body"=> $body,
+                "t"     => time(),
+            ]),
+        ];
+
+
+        if ($type == 1) {
+            $push_message = $notification;
+        } else {
+            $push_message = $transmission;
+        }
+
+        $data = [
+            "request_id"=> createUniqueNo('PA'),
+            "group_name"=> "",
+            "settings"=> [
+                "ttl"=> 7200000
+            ],
+            "audience"=> "all",
+            "push_message"=> $push_message,
+        ];
+
+        $response = $this->post('/push/all', $data, [
+            'token:'.$access_token
+        ]);
+
+        dump($response);
+        $data = json_decode($response, true);
+
+
+    }
+
+    /**
+     * 消息推送
+     * @param string $cid 设备ID
+     * @param string $title 消息标题
+     * @param string $body 消息内容
+     * @param int $type 1=通知,2=透传
+     * @param string $platform 平台
+     * @return bool
+     */
+    public function push( $cid,  $title,  $body, $ring_name, $type = 1,  $platform = 'android')
+    {
+        if (!$access_token = $this->auth()) {
+            return $this->error('ge tui token error');
+        }
+        dump($access_token);
+        // 通知
+        $notification = [
+            "notification" => [
+                "title"         => $title,
+                "body"          => $body,
+                "ring_name"     => $ring_name,
+                "channel_level" => 4,
+                "click_type"    => "startapp",//startapp:打开应用首页,payload:自定义消息内容启动应用,
+                "payload"       => json_encode(['t' => time()])
+            ],
+        ];
+
+        // 透传
+        $transmission = [
+            "transmission" => json_encode([
+                "title" => $title,
+                "body"  => $body,
+                "t"     => time(),
+            ], JSON_UNESCAPED_UNICODE)
+        ];
+
+        if ($type == 1) {
+            $push_message = $notification;
+        } else {
+            $push_message = $transmission;
+        }
+
+//        if ($platform == 'android') {
+//            $notify = $this->send($cid, $notification, $access_token, $push_channel ?? []);
+//            $trans  = $this->send($cid, $transmission, $access_token, $push_channel ?? []);
+//            if (!$notify || !$trans) {
+//                return false;
+//            }
+//            return true;
+//        }
+
+        if ($platform == 'ios') {
+            $push_channel = [
+                "ios" => [
+                    "type"       => "notify",
+                    "payload"    => json_encode([
+                        "title" => $title,
+                        "body"  => $body,
+                    ], JSON_UNESCAPED_UNICODE),
+                    "aps"        => [
+                        "alert"             => [
+                            "title" => $title,
+                            "body"  => $body,
+                        ],
+                        "content-available" => 0,
+                        "sound"             => "com.gexin.ios.silence",
+                        "category"          => "ACTIONABLE",
+                    ],
+                    "auto_badge" => "+1",
+                ]
+            ];
+        }
+
+        return $this->send($cid, $push_message, $access_token, $push_channel ?? []);
+    }
+
+    /**
+     * 发送
+     * @param $cid
+     * @param $push_message
+     * @param $access_token
+     * @return bool
+     */
+    private function send($cid, $push_message, $access_token, $push_channel)
+    {
+        $params = [
+            'request_id'   => createUniqueNo('GT'),
+            'settings'     => [
+                "ttl" => 7200000
+            ],
+            "audience"     => [
+                "cid" => [
+                    $cid
+                ]
+            ],
+            "push_message" => $push_message
+        ];
+
+        if (!empty($push_channel)) {
+            $params['push_channel'] = $push_channel;
+        }
+
+        $response = $this->post('/push/single/cid', $params, [
+            'token:'.$access_token
+        ]);
+        dump($response);
+        /*if ($response->getStatusCode() != 200) {
+            return $this->error($response->getReasonPhrase());
+        }
+        $json = $response->getBody()->getContents();
+        $body = json_decode($json, true);
+        return $this->success('操作成功', $body);*/
+    }
+
+    /**
+     * 获取token
+     * @return false|mixed
+     */
+    public function auth()
+    {
+        $timestamp = $this->ms_time();
+        $response  = $this->post('/auth', [
+            'sign'      => hash('sha256', "{$this->appKey}{$timestamp}{$this->masterSecret}"),
+            'timestamp' => $timestamp,
+            'appkey'    => $this->appKey
+        ]);
+        $data = json_decode($response, true);
+
+        $auth = isset($data['data']['token']) ? $data['data']['token'] : '';
+        return $auth;
+    }
+
+    private function post( $uri,  $params = [],  $header = [])
+    {
+        $common_header = ["Content-Type:application/json;charset=UTF-8","Connection: Keep-Alive"];
+        $header = array_merge($header,$common_header);
+
+        dump($header);
+        $url = $this->base_uri.$this->appId.$uri;
+        return curl_post($url,json_encode($params),$header);
+    }
+
+    private function ms_time()
+    {
+        list($ms, $sec) = explode(' ', microtime());
+        return intval((floatval($ms) + floatval($sec)) * 1000);
+    }
+
+
+}