zhaojs
2023-06-29 ef469c51ef6fec8986164471afb8f44a7d39dbeb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
 
namespace app\api\library;
 
 
/**
 * UniPush服务
 */
class UniPush
{
    private $appid,$appkey,$appsecret;
 
    public function __construct()
    {
        $config= config('site.push');
        $this->appid = $config['appid'];
        $this->appkey = $config['appkey'];
        $this->appsecret = $config['masterSecret'];
    }
 
 
    /**
     * 给指定用户推送消息
     *
     * @param [type] $cid 设备id
     * @param [type] $title 标题 
     * @param [type] $body  内容
     * @param [type] $path 路径
     * @return void
     */
    public static function pushToSingleByCids($cid,$title,$body,$click_type = "none",$path = ""){
        //创建API,APPID等配置参考 环境要求 进行获取
        $api = new \GTClient("https://restapi.getui.com",self::$appkey, self::$appid,self::$appsecret);
        //设置推送参数
        $push = new \GTPushRequest();
        $push->setRequestId(self::micro_time());
        $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($click_type);
        if($click_type == 'insert'){
            $intent = "intent:#Intent;action=android.intent.action.oppopush;launchFlags=0x14000000;component=com.huaxiangsh.app/io.dcloud.PandoraEntry;S.UP-OL-SU=true;S.payload="+$path+";end";
            $notify->setIntent($intent);
        }
        $message->setNotification($notify);
        $push->setPushMessage($message);
        $push->setCid($cid);
        //处理返回结果
        $result = $api->pushApi()->pushToSingleByCid($push);
        if($result['code'] != 0) return false;
        return true;
    }
 
    public static function micro_time()
    {
        list($usec, $sec) = explode(" ", microtime());
        $time = ($sec . substr($usec, 2, 3));
        return $time;
    }
 
 
 
}