JgPush.php
42.8 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
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
<?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;
/**
* 监管APP推送(unipush)
* @package app\api\controller\core
*/
class JgPush extends JGBase
{
/**
* 4. unipush推送通知消息至监管APP点击跳转通知详情
*/
public function pushMessage($id)
{
if (empty($id)) {
return array_callback(false, "id不能为空");
}
$messageModel = new \app\admin\model\inspection\Message();
$messageInfo = $messageModel->where("id", $id)->find();
$sum_count = 0;//总共需要推送的人
$react_count = 0;//实际推送人数
if (!empty($messageInfo)) {
$title = sub_str($messageInfo->title, 20);//通知消息标题,长度 ≤ 50
$content = sub_str($messageInfo->content, 50);//通知消息内容,长度 ≤ 256
$scene_type = "jump_to_message_page";
$scene_data = ["id" => $messageInfo->id, "type" => 0];//type 0未读
if (!empty($messageInfo->user_ids)) {
$user_ids = trim($messageInfo->user_ids, ",");
if (!empty($user_ids)) {
$sum_count = count(explode(",", $user_ids));
// 查找需要推送人的clientid
$w1 = [];
$w1["id"] = ["in", $user_ids];
$w1["clientid"] = ["neq", ""];
$clientid_list = Db::name("user")->where($w1)->column("clientid");
if (!empty($clientid_list)) {
$react_count = count($clientid_list);
//请求推送
$push = new \GTPushRequest();
$push->setRequestId($this->micro_time);
$set = new \GTSettings();
$set->setTtl(3600000);//默认1小时 消息离线时间设置,单位毫秒,-1表示不设离线,-1 ~ 3 * 24 * 3600 * 1000(3天)之间
$strategy = new \GTStrategy();
$strategy->setDefault(\GTStrategy::STRATEGY_THIRD_FIRST);//默认1 这里选的4 表示该消息优先从厂商通道下发,若消息内容在厂商通道代发失败后会从个推通道下发。
$set->setStrategy($strategy);
$push->setSettings($set);
$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 = ["messageTitle"=>$title,"messageContent"=>$content,"scene_type" => $scene_type, "data" => $scene_data];
$notify->setPayload(json_encode($array, JSON_UNESCAPED_UNICODE));
$message->setNotification($notify);
$push->setPushMessage($message);
//厂商推送消息参数
$pushChannel = new \GTPushChannel();
//ios
$ios = new \GTIos();
$ios->setType("notify");//默认notify voip:voip语音推送,notify:apns通知消息
$ios->setAutoBadge("1");//用于计算icon上显示的数字,还可以实现显示数字的自动增减,如“+1”、 “-1”、 “1” 等,计算结果将覆盖badge
$ios->setPayload(json_encode($array, JSON_UNESCAPED_UNICODE));//增加自定义的数据
// $ios->setApnsCollapseId("apnsCollapseId");//使用相同的apns-collapse-id可以覆盖之前的消息
//aps设置 ios的推送通知消息内容
$aps = new \GTAps();
$aps->setContentAvailable(0);//0表示普通通知消息(默认为0);1表示静默推送(无通知栏消息),静默推送时不需要填写其他参数。苹果建议1小时最多推送3条静默消息
$aps->setSound("default");//自定义铃声,设置为包含后缀名的完整文件名,示例值:ring.mp3;系统铃声,设置为:default;无声,设置为:com.gexin.ios.silence,或不填
// $aps->setCategory("category");//在客户端通知栏触发特定的action和button显示
// $aps->setThreadId("threadId");//ios的远程通知通过该属性对通知进行分组,仅支持iOS 12.0以上版本
//aps alert 通知消息
$alert = new \GTAlert();
$alert->setTitle($title);//通知消息标题
$alert->setBody($content);//通知消息内容
$aps->setAlert($alert);
$ios->setAps($aps);
$pushChannel->setIos($ios);
//安卓 厂商推送
$android = new \GTAndroid();
$ups = new \GTUps();
// $ups->setTransmission("ups Transmission");//透传消息内容,与notification、revoke 三选一,都填写时报错,长度 ≤ 3072
$thirdNotification = new \GTThirdNotification();
$thirdNotification->setTitle($title);//通知栏标题(长度建议取最小集)小米:title长度限制为50字;华为:title长度限制40字;魅族:title长度限制32字;OPPO:title长度限制32字;VIVO:title长度限制40英文字符
$thirdNotification->setBody($content);//通知栏内容(长度建议取最小集) 小米:content长度限制128字;华为:content长度小于1024字;魅族:content长度限制100字;OPPO:content长度限制200字;VIVO:content长度限制100个英文字符
$thirdNotification->setClickType(\GTThirdNotification::CLICK_TYPE_PAYLOAD);
// $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);//消息覆盖使用,两条消息的notify_id相同,新的消息会覆盖老的消息,范围:0-2147483647
$ups->addOption("HW","badgeAddNum",1);//华为
$ups->addOption("OP","channel","Default");//OPPO
$ups->addOption("OP","aaa","bbb");
$ups->addOption(null,"a","b");
$ups->setNotification($thirdNotification);
$android->setUps($ups);
$pushChannel->setAndroid($android);
$push->setPushChannel($pushChannel);
$push->setGroupName("message_" . $messageInfo->id);
$rr = $this->api->pushApi()->createListMsg($push);//【toList】创建消息;此接口用来创建消息体,并返回taskid,为批量推的前置步骤
if (!empty($rr)) {
if ($rr["code"] == 0) {
$taskid = $rr["data"]['taskid'];//任务编号,用于执行cid批量推和执行别名批量推,此taskid可以多次使用,有效期为用户设置的离线时间
if (!empty($taskid)) {
//执行cid批量推
$user = new \GTAudienceRequest();
$user->setTaskid($taskid);
$user->setCidList($clientid_list);
$result = $this->api->pushApi()->pushListByCid($user);
if (!empty($result)) {
if ($result['code'] == 0) {
// $this->success("推送成功", $result);
return array_callback(true, "推送成功", ["result" => $result, "sum_count" => $sum_count, "react_count" => $react_count]);
} else {
return array_callback(false, "推送异常[" . $result['code'] . $result['msg'] . "]");
}
} else {
return array_callback(false, "推送失败");
}
} else {
return array_callback(false, "消息体任务编号创建异常");
}
} else {
return array_callback(false, "创建消息体异常[" . $rr['code'] . $rr['msg'] . "]");
}
} else {
return array_callback(false, "创建消息体失败");
}
} else {
return array_callback(false, "通知人员clientid都为空,建议让TA们重新登录APP");
}
}
}
return array_callback(false, "请先选择需要通知的人员");
} else {
return array_callback(false, "未查找到需要推送的数据");
}
}
/**
* 5. unipush推送事务消息至监管APP点击跳转待办事项详情
*/
public function pushTask($id)
{
if (empty($id)) {
return array_callback(false, "id不能为空");
}
$taskModel = new \app\admin\model\inspection\Task();
$taskInfo = $taskModel->where("id", $id)->find();
$sum_count = 0;//总共需要推送的人
$react_count = 0;//实际推送人数
if (!empty($taskInfo)) {
$title = sub_str($taskInfo->title, 20);//通知消息标题,长度 ≤ 50
$content = sub_str($taskInfo->desc, 50);//通知消息内容,长度 ≤ 256
$scene_type = "jump_to_task_page";
$scene_data = ["id" => $taskInfo->id, "type" => "未读"];//type 0未读
if (!empty($taskInfo->user_id)) {
$user_ids = trim($taskInfo->user_id, ",");
if (!empty($user_ids)) {
$sum_count = count(explode(",", $user_ids));
// 查找需要推送人的clientid
$w1 = [];
$w1["id"] = ["in", $user_ids];
$w1["clientid"] = ["neq", ""];
$clientid_list = Db::name("user")->where($w1)->column("clientid");
if (!empty($clientid_list)) {
$react_count = count($clientid_list);
//请求推送
$push = new \GTPushRequest();
$push->setRequestId($this->micro_time);
$set = new \GTSettings();
$set->setTtl(3600000);//默认1小时 消息离线时间设置,单位毫秒,-1表示不设离线,-1 ~ 3 * 24 * 3600 * 1000(3天)之间
$strategy = new \GTStrategy();
$strategy->setDefault(\GTStrategy::STRATEGY_THIRD_FIRST);//默认1 这里选的4 表示该消息优先从厂商通道下发,若消息内容在厂商通道代发失败后会从个推通道下发。
$set->setStrategy($strategy);
$push->setSettings($set);
$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 = ["messageTitle"=>$title,"messageContent"=>$content,"scene_type" => $scene_type, "data" => $scene_data];
$notify->setPayload(json_encode($array, JSON_UNESCAPED_UNICODE));
$message->setNotification($notify);
$push->setPushMessage($message);
//厂商推送消息参数
$pushChannel = new \GTPushChannel();
//ios
$ios = new \GTIos();
$ios->setType("notify");//默认notify voip:voip语音推送,notify:apns通知消息
$ios->setAutoBadge("1");//用于计算icon上显示的数字,还可以实现显示数字的自动增减,如“+1”、 “-1”、 “1” 等,计算结果将覆盖badge
$ios->setPayload(json_encode($array, JSON_UNESCAPED_UNICODE));//增加自定义的数据
// $ios->setApnsCollapseId("apnsCollapseId");//使用相同的apns-collapse-id可以覆盖之前的消息
//aps设置 ios的推送通知消息内容
$aps = new \GTAps();
$aps->setContentAvailable(0);//0表示普通通知消息(默认为0);1表示静默推送(无通知栏消息),静默推送时不需要填写其他参数。苹果建议1小时最多推送3条静默消息
$aps->setSound("default");//自定义铃声,设置为包含后缀名的完整文件名,示例值:ring.mp3;系统铃声,设置为:default;无声,设置为:com.gexin.ios.silence,或不填
// $aps->setCategory("category");//在客户端通知栏触发特定的action和button显示
// $aps->setThreadId("threadId");//ios的远程通知通过该属性对通知进行分组,仅支持iOS 12.0以上版本
//aps alert 通知消息
$alert = new \GTAlert();
$alert->setTitle($title);//通知消息标题
$alert->setBody($content);//通知消息内容
$aps->setAlert($alert);
$ios->setAps($aps);
$pushChannel->setIos($ios);
//安卓 厂商推送
$android = new \GTAndroid();
$ups = new \GTUps();
// $ups->setTransmission("ups Transmission");//透传消息内容,与notification、revoke 三选一,都填写时报错,长度 ≤ 3072
$thirdNotification = new \GTThirdNotification();
$thirdNotification->setTitle($title);//通知栏标题(长度建议取最小集)小米:title长度限制为50字;华为:title长度限制40字;魅族:title长度限制32字;OPPO:title长度限制32字;VIVO:title长度限制40英文字符
$thirdNotification->setBody($content);//通知栏内容(长度建议取最小集) 小米:content长度限制128字;华为:content长度小于1024字;魅族:content长度限制100字;OPPO:content长度限制200字;VIVO:content长度限制100个英文字符
$thirdNotification->setClickType(\GTThirdNotification::CLICK_TYPE_PAYLOAD);
// $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);//消息覆盖使用,两条消息的notify_id相同,新的消息会覆盖老的消息,范围:0-2147483647
$ups->addOption("HW","badgeAddNum",1);//华为
$ups->addOption("OP","channel","Default");//OPPO
$ups->addOption("OP","aaa","bbb");
$ups->addOption(null,"a","b");
$ups->setNotification($thirdNotification);
$android->setUps($ups);
$pushChannel->setAndroid($android);
$push->setPushChannel($pushChannel);
$push->setGroupName("task_" . $taskInfo->id);
$rr = $this->api->pushApi()->createListMsg($push);//【toList】创建消息;此接口用来创建消息体,并返回taskid,为批量推的前置步骤
if (!empty($rr)) {
if ($rr["code"] == 0) {
$taskid = $rr["data"]['taskid'];//任务编号,用于执行cid批量推和执行别名批量推,此taskid可以多次使用,有效期为用户设置的离线时间
if (!empty($taskid)) {
//执行cid批量推
$user = new \GTAudienceRequest();
$user->setTaskid($taskid);
$user->setCidList($clientid_list);
$result = $this->api->pushApi()->pushListByCid($user);
if (!empty($result)) {
if ($result['code'] == 0) {
// $this->success("推送成功", $result);
return array_callback(true, "推送成功", ["result" => $result, "sum_count" => $sum_count, "react_count" => $react_count]);
} else {
return array_callback(false, "推送异常[" . $result['code'] . $result['msg'] . "]");
}
} else {
return array_callback(false, "推送失败");
}
} else {
return array_callback(false, "消息体任务编号创建异常");
}
} else {
return array_callback(false, "创建消息体异常[" . $rr['code'] . $rr['msg'] . "]");
}
} else {
return array_callback(false, "创建消息体失败");
}
} else {
return array_callback(false, "通知人员clientid都为空,建议让TA们重新登录APP");
}
}
}
return array_callback(false, "请先选择需要通知的人员");
} else {
return array_callback(false, "未查找到需要推送的数据");
}
}
/**
* 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服务器请求异常");
}
}
/**
* 14. unipush推送入侵报警至监管APP点击跳转实时监控详情
* "u_ids" => $u_ids,
* "indexCode" => $cameraIndexCode,
* "name" => Db::name("reservoir_hkws_hardware")->where("indexCode", $cameraIndexCode)->value("name"),
* "title" => "您有新的入侵报警消息,请及时查看",
* "desc" => "你好,系统监测反馈有新的入侵报警,请及时查看并处理"
*/
public function pushAlarm($param)
{
if (empty($param['u_ids'])) {
return array_callback(false, "推送用户不能为空");
}
$sum_count = 0;//总共需要推送的人
$react_count = 0;//实际推送人数
$title = $param['title'] ? sub_str($param['title'], 20) : "新的入侵报警待查看";//通知消息标题,长度 ≤ 50
$content = $param['desc'] ? sub_str($param['desc'], 50) : "您有新的入侵报警信息待查看";//通知消息内容,长度 ≤ 256
$scene_type = $param['jump_page'] ? $param['jump_page'] : "jump_to_rtmonitor_page";
$scene_data = ["indexCode" => $param['indexCode'], "name" => $param['name']];//
$user_ids = trim($param['u_ids'], ",");
if (!empty($user_ids)) {
$sum_count = count(explode(",", $user_ids));
// 查找需要推送人的clientid
$w1 = [];
$w1["id"] = ["in", $user_ids];
$w1["clientid"] = ["neq", ""];
$clientid_list = Db::name("user")->where($w1)->column("clientid");
if (!empty($clientid_list)) {
$react_count = count($clientid_list);
//请求推送
$push = new \GTPushRequest();
$push->setRequestId($this->micro_time);
$set = new \GTSettings();
$set->setTtl(3600000);//默认1小时 消息离线时间设置,单位毫秒,-1表示不设离线,-1 ~ 3 * 24 * 3600 * 1000(3天)之间
$strategy = new \GTStrategy();
$strategy->setDefault(\GTStrategy::STRATEGY_THIRD_FIRST);//默认1 这里选的4 表示该消息优先从厂商通道下发,若消息内容在厂商通道代发失败后会从个推通道下发。
$set->setStrategy($strategy);
$push->setSettings($set);
$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 = ["messageTitle"=>$title,"messageContent"=>$content,"scene_type" => $scene_type, "data" => $scene_data];
$notify->setPayload(json_encode($array, JSON_UNESCAPED_UNICODE));
$message->setNotification($notify);
$push->setPushMessage($message);
//厂商推送消息参数
$pushChannel = new \GTPushChannel();
//ios
$ios = new \GTIos();
$ios->setType("notify");//默认notify voip:voip语音推送,notify:apns通知消息
$ios->setAutoBadge("1");//用于计算icon上显示的数字,还可以实现显示数字的自动增减,如“+1”、 “-1”、 “1” 等,计算结果将覆盖badge
$ios->setPayload(json_encode($array, JSON_UNESCAPED_UNICODE));//增加自定义的数据
// $ios->setApnsCollapseId("apnsCollapseId");//使用相同的apns-collapse-id可以覆盖之前的消息
//aps设置 ios的推送通知消息内容
$aps = new \GTAps();
$aps->setContentAvailable(0);//0表示普通通知消息(默认为0);1表示静默推送(无通知栏消息),静默推送时不需要填写其他参数。苹果建议1小时最多推送3条静默消息
$aps->setSound("default");//自定义铃声,设置为包含后缀名的完整文件名,示例值:ring.mp3;系统铃声,设置为:default;无声,设置为:com.gexin.ios.silence,或不填
// $aps->setCategory("category");//在客户端通知栏触发特定的action和button显示
// $aps->setThreadId("threadId");//ios的远程通知通过该属性对通知进行分组,仅支持iOS 12.0以上版本
//aps alert 通知消息
$alert = new \GTAlert();
$alert->setTitle($title);//通知消息标题
$alert->setBody($content);//通知消息内容
$aps->setAlert($alert);
$ios->setAps($aps);
$pushChannel->setIos($ios);
//安卓 厂商推送
$android = new \GTAndroid();
$ups = new \GTUps();
// $ups->setTransmission("ups Transmission");//透传消息内容,与notification、revoke 三选一,都填写时报错,长度 ≤ 3072
$thirdNotification = new \GTThirdNotification();
$thirdNotification->setTitle($title);//通知栏标题(长度建议取最小集)小米:title长度限制为50字;华为:title长度限制40字;魅族:title长度限制32字;OPPO:title长度限制32字;VIVO:title长度限制40英文字符
$thirdNotification->setBody($content);//通知栏内容(长度建议取最小集) 小米:content长度限制128字;华为:content长度小于1024字;魅族:content长度限制100字;OPPO:content长度限制200字;VIVO:content长度限制100个英文字符
$thirdNotification->setClickType(\GTThirdNotification::CLICK_TYPE_PAYLOAD);
// $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);//消息覆盖使用,两条消息的notify_id相同,新的消息会覆盖老的消息,范围:0-2147483647
$ups->addOption("HW","badgeAddNum",1);//华为
$ups->addOption("OP","channel","Default");//OPPO
$ups->addOption("OP","aaa","bbb");
$ups->addOption(null,"a","b");
$ups->setNotification($thirdNotification);
$android->setUps($ups);
$pushChannel->setAndroid($android);
$push->setPushChannel($pushChannel);
$push->setGroupName("alarm_" . $param['alarm_id']);
$rr = $this->api->pushApi()->createListMsg($push);//【toList】创建消息;此接口用来创建消息体,并返回taskid,为批量推的前置步骤
if (!empty($rr)) {
if ($rr["code"] == 0) {
$taskid = $rr["data"]['taskid'];//任务编号,用于执行cid批量推和执行别名批量推,此taskid可以多次使用,有效期为用户设置的离线时间
if (!empty($taskid)) {
//执行cid批量推
$user = new \GTAudienceRequest();
$user->setTaskid($taskid);
$user->setCidList($clientid_list);
$result = $this->api->pushApi()->pushListByCid($user);
if (!empty($result)) {
if ($result['code'] == 0) {
// $this->success("推送成功", $result);
return array_callback(true, "推送成功", ["result" => $result, "sum_count" => $sum_count, "react_count" => $react_count]);
} else {
return array_callback(false, "推送异常[" . $result['code'] . $result['msg'] . "]");
}
} else {
return array_callback(false, "推送失败");
}
} else {
return array_callback(false, "消息体任务编号创建异常");
}
} else {
return array_callback(false, "创建消息体异常[" . $rr['code'] . $rr['msg'] . "]");
}
} else {
return array_callback(false, "创建消息体失败");
}
} else {
return array_callback(false, "通知人员clientid都为空,建议让TA们重新登录APP");
}
}
return array_callback(false, "请先选择需要通知的人员");
}
/**
* 4. 测试推送
*/
public function testPushToSingleByCid()
{
$param = $this->request->param();
$cid = $param['cid'];
$title = $param['title'];
$content = $param['content'];
$scene_type = "jump_to_message_page";
$scene_data = ["id" => 67, "type" => 0];//type 0未读
$array = ["messageTitle"=>$title,"messageContent"=>$content,"scene_type"=>$scene_type,"data"=>$scene_data];
if(empty($cid)){
$this->error("接收通知者id不能为空");
}
$push = new \GTPushRequest();
$push->setRequestId($this->micro_time);//填写10到32位的id,这里用时间自动生成
//设置setting
$set = new \GTSettings();
$set->setTtl(3600000);//默认1小时 消息离线时间设置,单位毫秒,-1表示不设离线,-1 ~ 3 * 24 * 3600 * 1000(3天)之间
// $set->setSpeed(1000);//默认0 定速推送,例如100,个推控制下发速度在100条/秒左右,0表示不限速
// $set->setScheduleTime(1591794372930);//默认无 定时推送时间,格式:毫秒时间戳
$strategy = new \GTStrategy();
$strategy->setDefault(\GTStrategy::STRATEGY_THIRD_FIRST);//默认1 这里选的4 表示该消息优先从厂商通道下发,若消息内容在厂商通道代发失败后会从个推通道下发。
// $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,(个推通道消息内容 通知消息(notification),仅支持安卓系统,iOS系统不展示个推通道下发的通知消息)
$message = new \GTPushMessage();
//通知(通知消息内容,仅支持安卓系统,iOS系统不展示个推通知消息,与transmission、revoke三选一,都填写时报错)
$notify = new \GTNotification();
$notify->setTitle($title);//*通知消息标题,长度 ≤ 50
$notify->setBody($content);//*通知消息内容,长度 ≤ 256
$notify->setBigText("bigText");//长文本消息内容,通知消息+长文本样式,与big_image二选一,两个都填写时报错,长度 ≤ 512
//与big_text二选一
// $notify->setBigImage("BigImage");//大图的URL地址,通知消息+大图样式, 与big_text二选一,两个都填写时报错,长度 ≤ 1024
// $notify->setLogo("push.png");//通知的图标名称,包含后缀名(需要在客户端开发时嵌入),如“push.png”,长度 ≤ 64
// $notify->setLogoUrl("LogoUrl");//通知图标URL地址,长度 ≤ 256
$notify->setChannelId("Default");//默认Default 通知渠道id,长度 ≤ 64
$notify->setChannelName("Default");//默认Default 通知渠道名称,长度 ≤ 64
$notify->setChannelLevel(3);//默认3 设置通知渠道重要性(可以控制响铃,震动,浮动,闪灯等等)
//android8.0以下
// 0,1,2:无声音,无振动,不浮动
//3:有声音,无振动,不浮动
//4:有声音,有振动,有浮动
//android8.0以上
//0:无声音,无振动,不显示;
//1:无声音,无振动,锁屏不显示,通知栏中被折叠显示,导航栏无logo;
//2:无声音,无振动,锁屏和通知栏中都显示,通知不唤醒屏幕;
//3:有声音,无振动,锁屏和通知栏中都显示,通知唤醒屏幕;
//4:有声音,有振动,亮屏下通知悬浮展示,锁屏通知以默认形式展示且唤醒屏幕;
$notify->setClickType("payload");//点击通知后续动作,点击通知后续动作;目前支持以下后续动作,1、intent:打开应用内特定页面url:打开网页地址。2、payload:自定义消息内容启动应用。3、payload_custom:自定义消息内容不启动应用。4、startapp:打开应用首页。5、none:纯通知,无后续动作
// $notify->setIntent("intent:#Intent;component=你的包名/你要打开的 activity 全路径;S.parm1=value1;S.parm2=value2;end");
// $notify->setUrl("url");//点击通知打开链接,长度 ≤ 1024
$notify->setPayload(json_encode($array,JSON_UNESCAPED_UNICODE));;//点击通知时,附加自定义透传消息,长度 ≤ 3072 click_type为payload/payload_custom时必填
// $notify->setNotifyId(22334455);//覆盖任务时会使用到该字段,两条消息的notify_id相同,新的消息会覆盖老的消息,范围:0-2147483647
// $notify->setRingName("ring_name");// 自定义铃声,请填写文件名,不包含后缀名(需要在客户端开发时嵌入),个推通道下发有效客户端SDK最低要求 2.14.0.0
$notify->setBadgeAddNum(1);//角标, 必须大于0, 个推通道下发有效此属性目前仅针对华为 EMUI 4.1 及以上设备有效;角标数字数据会和之前角标数字进行叠加;举例:角标数字配置1,应用之前角标数为2,发送此角标消息后,应用角标数显示为3。客户端SDK最低要求 2.14.0.0
$message->setNotification($notify);
//透传 ,与通知、撤回三选一
// $message->setTransmission("试试透传");
//撤回 撤回消息时使用(仅撤回个推通道消息),与notification、transmission三选一,都填写时报错(消息撤回请勿填写策略参数)
$revoke = new \GTRevoke();
$revoke->setForce(true);//在没有找到对应的taskId,是否把对应appId下所有的通知都撤回
$revoke->setOldTaskId("taskId");// 需要撤回的taskId
// $message->setRevoke($revoke);
$push->setPushMessage($message);
// $message->setDuration("1590547347000-1590633747000");//手机端通知展示时间段,格式为毫秒时间戳段,两个时间的时间差必须大于10分钟,例如:"1590547347000-1590633747000"
//厂商推送消息参数
$pushChannel = new \GTPushChannel();
//ios
$ios = new \GTIos();
$ios->setType("notify");//默认notify voip:voip语音推送,notify:apns通知消息
$ios->setAutoBadge("1");//用于计算icon上显示的数字,还可以实现显示数字的自动增减,如“+1”、 “-1”、 “1” 等,计算结果将覆盖badge
$ios->setPayload(json_encode($array, JSON_UNESCAPED_UNICODE));//增加自定义的数据
// $ios->setApnsCollapseId("apnsCollapseId");//使用相同的apns-collapse-id可以覆盖之前的消息
//aps设置 ios的推送通知消息内容
$aps = new \GTAps();
$aps->setContentAvailable(0);//0表示普通通知消息(默认为0);1表示静默推送(无通知栏消息),静默推送时不需要填写其他参数。苹果建议1小时最多推送3条静默消息
$aps->setSound("default");//自定义铃声,设置为包含后缀名的完整文件名,示例值:ring.mp3;系统铃声,设置为:default;无声,设置为:com.gexin.ios.silence,或不填
// $aps->setCategory("category");//在客户端通知栏触发特定的action和button显示
// $aps->setThreadId("threadId");//ios的远程通知通过该属性对通知进行分组,仅支持iOS 12.0以上版本
//aps alert 通知消息
$alert = new \GTAlert();
$alert->setTitle($title);//通知消息标题
$alert->setBody($content);//通知消息内容
// $alert->setActionLocKey("ActionLocKey");//(用于多语言支持)指定执行按钮所使用的Localizable.strings
// $alert->setLocKey("LocKey");//(用于多语言支持)指定Localizable.strings文件中相应的key
// $alert->setLocArgs(array("LocArgs1","LocArgs2"));//如果loc-key中使用了占位符,则在loc-args中指定各参数
// $alert->setLaunchImage("LaunchImage");//指定启动界面图片名
// $alert->setTitleLocKey("TitleLocKey");// (用于多语言支持)对于标题指定执行按钮所使用的Localizable.strings,仅支持iOS8.2以上版本
// $alert->setTitleLocArgs(array("TitleLocArgs1","TitleLocArgs2"));//对于标题,如果loc-key中使用的占位符,则在loc-args中指定各参数,仅支持iOS8.2以上版本
// $alert->setSubtitle("Subtitle");//通知子标题,仅支持iOS8.2以上版本
// $alert->setSubtitleLocKey("SubtitleLocKey");//当前本地化文件中的子标题字符串的关键字,仅支持iOS8.2以上版本
// $alert->setSubtitleLocArgs(array("subtitleLocArgs1","subtitleLocArgs2"));//当前本地化子标题内容中需要置换的变量参数 ,仅支持iOS8.2以上版本
$aps->setAlert($alert);
$ios->setAps($aps);
//多媒体设置
// $multimedia = new \GTMultimedia();
// $multimedia->setUrl("url");//多媒体资源地址
// $multimedia->setType(1);//资源类型(1.图片,2.音频,3.视频)
// $multimedia->setOnlyWifi(false);//是否只在wifi环境下加载,如果设置成true,但未使用wifi时,会展示成普通通知
// $multimedia2 = new \GTMultimedia();
// $multimedia2->setUrl("url2");
// $multimedia2->setType(2);
// $multimedia2->setOnlyWifi(true);
// $ios->setMultimedia(array($multimedia));
// $ios->addMultimedia($multimedia2);
$pushChannel->setIos($ios);
//安卓 厂商推送
$android = new \GTAndroid();
$ups = new \GTUps();
// $ups->setTransmission("ups Transmission");//透传消息内容,与notification、revoke 三选一,都填写时报错,长度 ≤ 3072
$thirdNotification = new \GTThirdNotification();
$thirdNotification->setTitle($title);//通知栏标题(长度建议取最小集)小米:title长度限制为50字;华为:title长度限制40字;魅族:title长度限制32字;OPPO:title长度限制32字;VIVO:title长度限制40英文字符
$thirdNotification->setBody($content);//通知栏内容(长度建议取最小集) 小米:content长度限制128字;华为:content长度小于1024字;魅族:content长度限制100字;OPPO:content长度限制200字;VIVO:content长度限制100个英文字符
$thirdNotification->setClickType(\GTThirdNotification::CLICK_TYPE_PAYLOAD);
// $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);//消息覆盖使用,两条消息的notify_id相同,新的消息会覆盖老的消息,范围:0-2147483647
$ups->addOption("HW","badgeAddNum",1);//华为
$ups->addOption("OP","channel","Default");//OPPO
$ups->addOption("OP","aaa","bbb");
$ups->addOption(null,"a","b");
$ups->setNotification($thirdNotification);
$android->setUps($ups);
$pushChannel->setAndroid($android);
$push->setPushChannel($pushChannel);
$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服务器请求异常");
}
}
}