chengkun
2025-05-26 8f3df543230cd4403368b39b9bbe5726d11a0284
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
require_once(dirname(__FILE__) . '/' . 'GTClient.php');
 
class PhpyunPush{
    private $api;
    
    function __construct($config) {
        $this->api = new GTClient(null,$config['appkey'],$config['appid'],$config['masterSecret'], $config['ssl']);
        
    }
    /**
     * 总的单推消息的接口
     * param1(推送消息)  :   ['title' => "通知标题",'content' => "通知内容" , 'payload' => "通知去干嘛这里可以自定义"]
     * param2(接收人)   :   ['cid' => "",'device_token' => "" , system=""]
     * */
    public function pushIGtMsg($msg , $to ){
        
        if($to['system'] == 1){
            $push = $this->getParamForIOS($msg);
        }else if($to['system'] == 2){
            $push = $this->getParamForAndroid($msg);
        }
        $push->setCid($to['cid']);
        $rep = $this->api->pushApi()->pushToSingleByCid($push);
        
        return $rep;
    }
    function getParamForIOS($msg){
        $push = new GTPushRequest();
        $push->setRequestId($this->micro_time());
        //设置setting
        $set = new GTSettings();
        $set->setTtl(3600000);
        //$set->setSpeed(1000);
        //$set->setScheduleTime(1591794372930);
        
        // 推送模式
        $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);
        $set->setStrategy($strategy);
        $push->setSettings($set);
        
        $notify = new GTNotification();
        $notify->setTitle($msg['title']); // 通知消息标题
        $notify->setBody($msg['content']); // 通知消息内容
        $notify->setClickType("payload");
        $notify->setPayload($msg['payload']);
        
        //设置PushMessage,
        $message = new GTPushMessage();
        $message->setNotification($notify);
        $push->setPushMessage($message);
        //$message->setDuration("1590547347000-1590633747000");
         //厂商推送消息参数
         $pushChannel = new GTPushChannel();
         //ios
         $ios = new GTIos();
         $ios->setType("notify");
         $ios->setAutoBadge("+1");
         $ios->setPayload($msg['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($msg['title']);
         $alert->setBody($msg['content']);
         
         // 多语言支持
//          $alert->setActionLocKey("ActionLocKey");
//          $alert->setLocKey("LocKey");
//          $alert->setLocArgs(array("LocArgs1","LocArgs2"));
         
         //$alert->setLaunchImage("LaunchImage"); // 指定启动界面图片名
         
//          $alert->setTitleLocKey("TitleLocKey");
//          $alert->setTitleLocArgs(array("TitleLocArgs1","TitleLocArgs2"));
 
         // 通知子标题,仅支持iOS8.2以上版本
//          $alert->setSubtitle("Subtitle");
//          $alert->setSubtitleLocKey("SubtitleLocKey");
//          $alert->setSubtitleLocArgs(array("subtitleLocArgs1","subtitleLocArgs2"));
         $aps->setAlert($alert);
         $ios->setAps($aps);
         
         $pushChannel->setIos($ios);
         $push->setPushChannel($pushChannel);
         
        return $push;
    }
    function getParamForAndroid($msg){
        $push = new GTPushRequest();
        $push->setRequestId($this->micro_time());
        //设置setting
        $set = new GTSettings();
        $set->setTtl(3600000);
        //$set->setSpeed(1000);
        //$set->setScheduleTime(1591794372930);
        
        // 推送模式
        $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);
        $set->setStrategy($strategy);
        $push->setSettings($set);
        //设置PushMessage,
        $message = new GTPushMessage();
        // 通知、透传、撤回三选一
       
        //通知
        $notify = new GTNotification();
        $notify->setTitle($msg['title']); // 通知消息标题
        $notify->setBody($msg['content']); // 通知消息内容
        $notify->setClickType("payload");
        $notify->setPayload($msg['payload']);
        
        // 点击跳转地址
//         $notify->setIntent("intent:#Intent;component=你的包名/你要打开的 activity 全路径;S.parm1=value1;S.parm2=value2;end");
//         $notify->setUrl("url");
 
        $message->setNotification($notify);
        //透传
        //$message->setTransmission($msg);
        /*
        //撤回
        $revoke = new GTRevoke();
        $revoke->setForce(true);
        $revoke->setOldTaskId("taskId");
        $message->setRevoke($revoke);
        */
        $push->setPushMessage($message);
        //$message->setDuration("1590547347000-1590633747000");
        /*
        //厂商推送消息参数
        $pushChannel = new GTPushChannel();
        
        //安卓厂商通道
        $android = new GTAndroid();
        $ups = new GTUps();
        //    $ups->setTransmission("ups Transmission");
        $thirdNotification = new GTThirdNotification();
        $thirdNotification->setTitle("title".$this->micro_time());
        $thirdNotification->setBody("body".$this->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);
        */
        return $push;
    }
    
    function micro_time()
    {
        list($usec, $sec) = explode(" ", microtime());
        $time = ($sec . substr($usec, 2, 3));
        return $time;
    }
}
?>