Push.php
2.3 KB
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
<?php
/**
* Created by PhpStorm.
* User: Kevin
* Date: 2022/03/16 1500
* Time: 15:00
*/
namespace app\api\controller\getui;
use think\Db;
use think\Request;
/**
* 推送(unipush)
* @package app\api\controller\core
*/
class Push extends Base
{
/**
* 2. 推送API_根据cid进行单推
*/
public function pushToSingleByCid()
{
$param = $this->request->param();
$cid = $param['cid'];
$title = $param['title'];
$content = $param['content'];
$scene_type = $param['scene_type']?$param['scene_type']:"default";
if(empty($cid)){
$this->error("接收通知者id不能为空");
}
$push = new \GTPushRequest();
$push->setRequestId($this->micro_time);
$message = new \GTPushMessage();
$notify = new \GTNotification();
$notify->setTitle($title);
$notify->setBody($content);
//点击通知后续动作,目前支持以下后续动作:
//1、intent:打开应用内特定页面url:打开网页地址。2、payload:自定义消息内容启动应用。3、payload_custom:自定义消息内容不启动应用。4、startapp:打开应用首页。5、none:纯通知,无后续动作
$notify->setClickType("payload");
$array = ["scene_type"=>$scene_type];
$notify->setPayload(json_encode($array,JSON_UNESCAPED_UNICODE));
$message->setNotification($notify);
$push->setPushMessage($message);
$push->setCid($cid);
//处理返回结果
$result = $this->api->pushApi()->pushToSingleByCid($push);
$status ="";
if (!empty($result)) {
if ($result['code'] == 0) {
$data = $result['data'];
if(!empty($data)){
foreach ($data as $k=>$v){
if(!empty($v[$cid])){
$status = $v[$cid];
break;
}
}
}
$this->success("请求成功", $status);
} else {
$this->error("请求异常[" . $result['code'] . $result['msg'] . "]");
}
} else {
$this->error("unipush服务器请求异常");
}
}
}