-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetui.go
More file actions
997 lines (904 loc) · 24.5 KB
/
getui.go
File metadata and controls
997 lines (904 loc) · 24.5 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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
/*
个推client封装
sam
2022-09-01
pushClient:=NewPushClient(....)
resp,err:=pushClient.PushAll(...)
*/
package getuipush
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/zituocn/logx"
"strings"
"time"
"github.com/zituocn/getui-push/models"
"github.com/zituocn/gow/lib/goredis"
)
var (
// TTL 消息存放时间
TTL = 86400000 // 1天: 1 * 24 * 3600 * 1000
ctx = context.Background()
// expTime token 在redis中的过期时间
expTime = time.Hour * 20
)
// PushConfig 配置
//
// 从个推获取
type PushConfig struct {
AppId string
AppKey string
AppSecret string
MasterSecret string
}
// PushStore token存储配置
//
// redis配置信息
type PushStore struct {
Host string //redis host
Port int // redis port
DB int // redis db
Password string //redis password
Key string //存储key名称
}
// HarmonyConfig 鸿蒙包名等配置
type HarmonyConfig struct {
BundleName string //包名
AbilityName string //鸿蒙配置
Action string //鸿蒙配置
Uri string //鸿蒙配置
}
type AppConfig struct {
Harmony *HarmonyConfig
}
// PushClient 个推 push client
type PushClient struct {
*PushConfig
*PushStore
*AppConfig
}
// NewPushClient 返回个推实例并初始化redis信息
func NewPushClient(conf *PushConfig, store *PushStore, app *AppConfig, toDebug bool) (client *PushClient, err error) {
if conf == nil {
err = errors.New("配置为空")
return
}
if store == nil {
err = errors.New("存储存储配置为空")
return
}
if conf.AppId == "" || conf.AppSecret == "" || conf.AppKey == "" {
err = errors.New("个推参数配置不完整")
return
}
if store.Host == "" || store.Port == 0 || store.DB < 0 {
err = errors.New("存储参数配置不完整")
return
}
if toDebug {
ToDebug = true
}
client = &PushClient{
PushConfig: conf,
PushStore: store,
AppConfig: app,
}
err = goredis.InitDefaultDB(&goredis.RedisConfig{
Host: store.Host,
Port: store.Port,
Pool: 100,
Password: store.Password,
Name: "gt",
DB: store.DB,
})
if err != nil {
return
}
return
}
// GetToken 获取token
//
// 从redis中或api中获取
func (g *PushClient) GetToken() (token string, err error) {
rdb := goredis.GetRDB()
token, err = rdb.Get(ctx, g.Key).Result()
if err != nil {
logx.Errorf("%s 在redis中获取token失败 :%s", NAME, err.Error())
}
if token == "" {
token, err = getToken(g.AppId, g.AppKey, g.MasterSecret)
if err != nil {
err = fmt.Errorf("%s 从API获取token失败: %s", NAME, err.Error())
return
}
//_, err = rdb.SetEX(ctx, g.Key, token, time.Duration(expTime)).Result()
_, err = rdb.SetEx(ctx, g.Key, token, time.Duration(expTime)).Result()
if err != nil {
logx.Errorf("%s 在redis中存储token失败 :%s", NAME, err.Error())
}
}
return
}
/*
===============================================================
绑定用户别名
===============================================================
*/
// BindAlias 绑定别名
func (g *PushClient) BindAlias(param *models.Alias) (resp *models.Response, err error) {
if param == nil || param.Cid == "" {
err = errors.New("param未设置或cid为空")
return
}
token, err := g.GetToken()
if err != nil {
return
}
dataList := make([]*models.Alias, 0)
dataList = append(dataList, param)
aliasParam := &models.AliasParam{
DataList: dataList,
}
return bindAlias(g.AppId, token, aliasParam)
}
// UnBindAlias 解绑别名
//
// cid与alias成对出现
func (g *PushClient) UnBindAlias(param *models.Alias) (resp *models.Response, err error) {
if param == nil || param.Cid == "" {
err = errors.New("param未设置或cid为空")
return
}
token, err := g.GetToken()
if err != nil {
return
}
dataList := make([]*models.Alias, 0)
dataList = append(dataList, param)
aliasParam := &models.AliasParam{
DataList: dataList,
}
return unBindAlias(g.AppId, token, aliasParam)
}
// UnBindAllAlias 解绑所有与该别名绑定的cid
func (g *PushClient) UnBindAllAlias(alias string) (resp *models.Response, err error) {
if alias == "" {
err = errors.New("alias为空")
return
}
token, err := g.GetToken()
if err != nil {
return
}
return unBindAllAlias(g.AppId, token, alias)
}
// GetUserCount 查询用户总量
func (g *PushClient) GetUserCount(tags []*models.Tag) (resp *models.Response, err error) {
if len(tags) <= 0 {
err = errors.New("tag为空")
return
}
token, err := g.GetToken()
if err != nil {
return
}
return getUserCount(g.AppId, token, tags)
}
/*
===============================================================
绑定自定义标签
===============================================================
*/
// BindTags 一个用户绑定一批标签
//
// cid表示用户
func (g *PushClient) BindTags(cid string, param *models.CustomTagsParam) (resp *models.Response, err error) {
if cid == "" {
err = errors.New("cid为空")
return
}
if param == nil {
err = errors.New("param为空")
return
}
if len(param.CustomTag) == 0 {
err = errors.New("自定义标签长度为0")
return
}
if len(param.CustomTag) > 100 {
err = errors.New("自定义标签长度大于100个")
return
}
token, err := g.GetToken()
if err != nil {
return
}
return bindTags(g.AppId, token, cid, param)
}
/*
===============================================================
查询相关接口
使用Response.data的返回json,需要进一步格式化展示
===============================================================
*/
// SearchTags 查询某个用户已经绑定的标签
/*
{
"7399c780f73ac4046d930dd2b4edf3b4": [
"VIP用户 文科 手机登录 本科二批 iOS guangdong"
]
}
*/
func (g *PushClient) SearchTags(cid string) (resp *models.Response, err error) {
if cid == "" {
err = errors.New("cid为空")
return
}
token, err := g.GetToken()
if err != nil {
return
}
return searchTags(g.AppId, token, cid)
}
// SearchStatus 查询某个用户的状态,是否在线,上次在线时间等
// 根据cid查询
/*
{
"294d4da8b52d909ed30d261baf91d2d2": {
"last_login_time": "1663897775596",
"status": "offline"
}
}
*/
func (g *PushClient) SearchStatus(cid string) (resp *models.Response, err error) {
if cid == "" {
err = errors.New("cid为空")
return
}
token, err := g.GetToken()
if err != nil {
return
}
return searchStatus(g.AppId, token, cid)
}
// SearchUser 查询用户信息
// 根据cid查询
/*
{
"validCids": {
"294d4da8b52d909ed30d261baf91d2d2": {
"client_app_id": "7a4W8IrA3rAHxlJunzfTe",
"package_name": "ymzy-dream-iOS",
"device_token": "c2a73ad014d19111fef4454ebe19c811fd9c994f1e9f767318667adf9d0bbb69,,",
"phone_type": 2,
"phone_model": "iPhone14,2",
"notification_switch": true,
"create_time": "2022-03-03 14:10:46",
"login_freq": 21,
"brand": "iphone"
}
}
}
*/
func (g *PushClient) SearchUser(cid string) (resp *models.Response, err error) {
if cid == "" {
err = errors.New("cid为空")
return
}
token, err := g.GetToken()
if err != nil {
return
}
resp, err = searchUser(g.AppId, token, cid)
if err != nil {
return
}
return
}
// SearchAliasByCid 按cid查别名
// 即这台设备上登录过哪些帐号
/*
{"alias":"255617"}
*/
func (g *PushClient) SearchAliasByCid(cid string) (resp *models.Response, err error) {
if cid == "" {
err = errors.New("cid为空")
return
}
token, err := g.GetToken()
if err != nil {
return
}
return searchAliasByCid(g.AppId, token, cid)
}
// SearchCidByAlias 按alias查cid
// 即这个alias绑定过哪些设备
/*
{
"cid": [
"1fb427ab8f93a6de4655f4a15add51d2",
"699214926b118e9512a9330423fbaf5f"
]
}
*/
func (g *PushClient) SearchCidByAlias(alias string) (resp *models.Response, err error) {
if alias == "" {
err = errors.New("别名为空")
return
}
token, err := g.GetToken()
if err != nil {
return
}
return searchCidByAlias(g.AppId, token, alias)
}
// SearchTaskDetailByCid 可以查询某任务下某cid的具体实时推送路径情况
//
// 用于跟踪某个用户的消息到达情况
// 此接口需要SVIP权限,暂时不可用
func (g *PushClient) SearchTaskDetailByCid(cid, taskId string) (resp *models.TaskDetailResp, err error) {
if cid == "" {
err = errors.New("cid为空")
return
}
if taskId == "" {
err = errors.New("taskid为空")
return
}
token, err := g.GetToken()
if err != nil {
return
}
return searchTaskDetailByCid(g.AppId, token, cid, taskId)
}
// ReportPushTask 获取推送结果(含自定义事件)可查询消息可下发数、下发数,接收数、展示数、点击数等结果
//
// 用于跟踪某个用户的消息到达情况
func (g *PushClient) ReportPushTask(taskId string) (resp *models.Response, err error) {
if taskId == "" {
err = errors.New("taskid为空")
return
}
token, err := g.GetToken()
if err != nil {
return
}
return reportPushTask(g.AppId, token, taskId)
}
/*
===============================================================
推给所有人
===============================================================
*/
// PushAll 推送给所有人
//
// scheduleTime 定时推送时间戳,为0时,不定时
func (g *PushClient) PushAll(msgType, scheduleTime int, payload *models.CustomMessage) (resp *models.Response, err error) {
token, err := g.GetToken()
if err != nil {
return
}
pushMessage, pushChannel, setting, err := g.getPushMessageAndChannel(msgType, scheduleTime, payload)
if err != nil {
return
}
pushParam := &models.PushParam{
GroupName: getGroupName(),
RequestId: getRandString(),
Setting: setting,
Audience: "all",
PushMessage: pushMessage,
PushChannel: pushChannel,
}
resp, err = pushApp(g.AppId, token, pushParam)
if err != nil {
return
}
return
}
/*
===============================================================
推给指定端类型
===============================================================
*/
// PushAllByClient 推送给不同的客户端
//
// clientType 客户端类型,只能选1种
// scheduleTime 定时推送时间戳,为0时,不定时
func (g *PushClient) PushAllByClient(msgType, scheduleTime int, clientType ClientType, payload *models.CustomMessage) (resp *models.Response, err error) {
token, err := g.GetToken()
if err != nil {
return
}
pushMessage, pushChannel, setting, err := g.getPushMessageAndChannel(msgType, scheduleTime, payload)
if err != nil {
return
}
var phones []string
switch clientType {
case Android:
phones = []string{"android"}
case IOS:
phones = []string{"ios"}
case WechatAPP:
//TODO:
}
tag := make([]*models.Tag, 0)
tag = append(tag, &models.Tag{
Key: "phone_type",
Values: phones,
OptType: "or",
})
audience := struct {
Tag []*models.Tag `json:"tag"`
}{}
audience.Tag = tag
pushParam := &models.PushParam{
GroupName: getGroupName(),
RequestId: getRandString(),
Setting: setting,
Audience: audience,
PushMessage: pushMessage,
PushChannel: pushChannel,
}
resp, err = pushAppByClient(g.AppId, token, pushParam)
if err != nil {
return
}
return
}
/*
===============================================================
推给某一个用户
===============================================================
*/
// PushSingleByCid 单推给某一个用户
//
// cid = 用户的cid信息
// channelType = 通道类型
func (g *PushClient) PushSingleByCid(msgType int, cid string, payload *models.CustomMessage) (resp *models.Response, err error) {
token, err := g.GetToken()
if err != nil {
return
}
pushMessage, pushChannel, setting, err := g.getPushMessageAndChannel(msgType, 0, payload)
if err != nil {
return
}
audience := struct {
Cid []string `json:"cid"`
}{}
audience.Cid = []string{cid}
pushParam := &models.PushParam{
GroupName: getGroupName(),
RequestId: getRandString(),
Setting: setting,
Audience: audience,
PushMessage: pushMessage,
PushChannel: pushChannel,
}
resp, err = pushSingleByCid(g.AppId, token, pushParam)
if err != nil {
return
}
return
}
/*
===============================================================
推给某一个用户
===============================================================
*/
// PushSingleByAlias 单推给某一个用户
//
// alias = 用户的alias
// channelType = 通道类型
func (g *PushClient) PushSingleByAlias(msgType int, alias string, payload *models.CustomMessage) (resp *models.Response, err error) {
token, err := g.GetToken()
if err != nil {
return
}
pushMessage, pushChannel, setting, err := g.getPushMessageAndChannel(msgType, 0, payload)
if err != nil {
return
}
audience := struct {
Alias []string `json:"alias"`
}{}
audience.Alias = []string{alias}
pushParam := &models.PushParam{
GroupName: getGroupName(),
RequestId: getRandString(),
Setting: setting,
Audience: audience,
PushMessage: pushMessage,
PushChannel: pushChannel,
}
resp, err = pushSingleByAlias(g.AppId, token, pushParam)
if err != nil {
return
}
return
}
/*
===============================================================
按cid群推
===============================================================
*/
// PushListByCid 按cid群推消息
//
// 当cid长度大于1000时,会分页循环进行推送
func (g *PushClient) PushListByCid(msgType int, cid []string, payload *models.CustomMessage) (data []*models.Response, err error) {
if len(cid) == 0 {
err = errors.New("cid长度为0")
return
}
token, err := g.GetToken()
if err != nil {
return
}
pushMessage, pushChannel, setting, err := g.getPushMessageAndChannel(msgType, 0, payload)
if err != nil {
return
}
pushParam := &models.PushParam{
GroupName: getGroupName(),
RequestId: getRandString(),
Setting: setting,
PushMessage: pushMessage,
PushChannel: pushChannel,
}
// 创建消息
resp, err := createPushMessage(g.AppId, token, pushParam)
if err != nil {
err = fmt.Errorf("%s 保存消息失败: %s", NAME, err.Error())
return
}
//返回的taskId
taskId := resp.Data
pageCount := getPageCount(limit, len(cid))
data = make([]*models.Response, 0)
// 分页群推
for i := 1; i <= pageCount; i++ {
list := getSplitCid(cid, i, limit)
pushListParam := &models.PushListParam{
TaskId: taskId,
}
pushListParam.Audience.Cid = list //每次的推送列表
pushListParam.IsAsync = false //不异步
respList, err := pushListByCid(g.AppId, token, pushListParam)
if err != nil {
logx.Errorf("%s 按cid群推失败: %s", NAME, err.Error())
}
data = append(data, respList)
time.Sleep(time.Microsecond * 500) //休眠500ms
}
return
}
/*
===============================================================
根据条件筛选用户推送
===============================================================
*/
// PushAllByCustomTag 对指定应用的符合筛选条件的用户群发推送消息。支持定时、定速功能
//
// 此接口频次限制100次/天,每分钟不能超过5次(推送限制和接口执行群推共享限制),定时推送功能需要申请开通才可以使用
// scheduleTime 定时推送时间戳,为0时,不定时
// customTag 内的标签是交集的关系
func (g *PushClient) PushAllByCustomTag(msgType, scheduleTime int, customTag []string, payload *models.CustomMessage) (resp *models.Response, err error) {
if len(customTag) == 0 {
err = errors.New("自定义标签长度为0")
return
}
token, err := g.GetToken()
if err != nil {
return
}
pushMessage, pushChannel, setting, err := g.getPushMessageAndChannel(msgType, scheduleTime, payload)
if err != nil {
return
}
tags := make([]*models.Tag, 0)
tags = append(tags, &models.Tag{
Key: "custom_tag",
Values: customTag,
OptType: "or",
})
audience := struct {
Tag []*models.Tag `json:"tag"`
}{}
audience.Tag = tags
pushParam := &models.PushParam{
GroupName: getGroupName(),
RequestId: getRandString(),
Setting: setting,
Audience: audience,
PushMessage: pushMessage,
PushChannel: pushChannel,
}
resp, err = pushAppByTag(g.AppId, token, pushParam)
if err != nil {
return
}
return
}
// PushAllByLogicTags 对指定应用的符合筛选条件的用户群发推送消息。支持定时、定速功能
//
// 此接口频次限制100次/天,每分钟不能超过5次(推送限制和接口执行群推共享限制),定时推送功能需要申请开通才可以使用
// scheduleTime 定时推送时间戳,为0时,不定时
// tags为[]*models.Tag,需要自己构建tag表达式
// see @https://docs.getui.com/getui/server/rest_v2/push/
func (g *PushClient) PushAllByLogicTags(msgType, scheduleTime int, tags []*models.Tag, payload *models.CustomMessage) (resp *models.Response, err error) {
if len(tags) == 0 {
err = errors.New("标签表达式长度为0")
return
}
token, err := g.GetToken()
if err != nil {
return
}
pushMessage, pushChannel, setting, err := g.getPushMessageAndChannel(msgType, scheduleTime, payload)
if err != nil {
return
}
audience := struct {
Tag []*models.Tag `json:"tag"`
}{}
audience.Tag = tags
pushParam := &models.PushParam{
GroupName: getGroupName(),
RequestId: getRandString(),
Setting: setting,
Audience: audience,
PushMessage: pushMessage,
PushChannel: pushChannel,
}
resp, err = pushAppByTag(g.AppId, token, pushParam)
if err != nil {
return
}
return
}
/*
===============================================================
使用标签快速推送
===============================================================
*/
// PushAppByFastCustomTag 使用标签快速推送
//
// tag 为某一个标签名
// scheduleTime 为定时任务的时间戳
// 此接口需要SVIP才有使用权限
func (g *PushClient) PushAppByFastCustomTag(msgType, scheduleTime int, tag string, payload *models.CustomMessage) (resp *models.Response, err error) {
if tag == "" {
err = errors.New("自定义标签长度为0")
return
}
token, err := g.GetToken()
if err != nil {
return
}
pushMessage, pushChannel, setting, err := g.getPushMessageAndChannel(msgType, scheduleTime, payload)
if err != nil {
return
}
audience := struct {
FastCustomTag string `json:"fast_custom_tag"`
}{}
audience.FastCustomTag = tag
pushParam := &models.PushParam{
GroupName: getGroupName(),
RequestId: getRandString(),
Setting: setting,
Audience: audience,
PushMessage: pushMessage,
PushChannel: pushChannel,
}
resp, err = pushAppByFastCustomTag(g.AppId, token, pushParam)
if err != nil {
return
}
return
}
/*
===============================================================
使用标签快速推送
===============================================================
*/
// StopTask 停止推送任务
//
// 对正处于推送状态,或者未接收的消息停止下发(只支持批量推和群推任务)
func (g *PushClient) StopTask(taskId string) (resp *models.Response, err error) {
if taskId == "" {
err = errors.New("taskid为空")
return
}
token, err := g.GetToken()
if err != nil {
return
}
return stopTask(g.AppId, token, taskId)
}
/*
private
*/
// getPushMessageAndChannel 构造消息
//
// channelType 通道
// msgType 消息类型
// scheduleTime 定时任务的时间戳
// payload 消息结构体
func (m *PushClient) getPushMessageAndChannel(msgType int, scheduleTime int, payload *models.CustomMessage) (pushMessage *models.PushMessage, pushChannel *models.PushChannel, setting *models.Setting, err error) {
payload.Title = strings.TrimSpace(payload.Title)
pushInfo, err := json.Marshal(payload)
if err != nil {
return
}
// 参数配置
setting = &models.Setting{
TTL: TTL,
}
setting.Strategy.IOS = 2
setting.Strategy.Default = 1
setting.Strategy.HW = 1
setting.Strategy.HO = 1
setting.Strategy.HOSHW = 1
setting.Strategy.OP = 1
setting.Strategy.VV = 1
setting.Strategy.XM = 1
if scheduleTime > 0 {
setting.ScheduleTime = scheduleTime
}
// 个推消息,走透传模式
// TODO:此处可测试是否可走 通知消息模式
pushMessage = &models.PushMessage{
Transmission: string(pushInfo),
}
// 个推消息,走通知模式
// pushMessage = &models.PushMessage{
// Notification: &models.Notification{
// Title: payload.Title,
// Body: payload.Content,
// //LogoUrl: "https://lib.ymzy.cn/ymzy/wap/static/images/down_app/logo.png",
// ClickType: "intent",
// Intent: getIntent(payload.Url),
// NotifyId: uint(time.Now().Unix()),
// ChannelLevel: 4,
// BadgeAddNum: 1,
// },
// }
// iOS消息配置
ios := &models.IOSChannel{
Payload: string(pushInfo),
Type: "notify",
AutoBadge: "+1",
}
ios.Aps.ContentAvailable = 0 //通知消息 =1时为静默消息
ios.Aps.Sound = "default" //铃声
ios.Aps.Alert.Title = payload.Title
ios.Aps.Alert.Body = payload.Content
// android 消息配置
android := &models.AndroidChannel{}
//走厂商的通知消息
android.Ups.Notification = &models.UPSNotification{
Title: payload.Title,
Body: payload.Content,
ClickType: "intent", //打开应用内特定页面(厂商都支持)
Intent: getIntent(payload.Url),
NotifyId: uint(time.Now().Unix()),
}
// android 离线推送通道
// 以下为厂商配置
//根据最新的消息推送规定,需要按照指定的消息类型推送,不再仅分为 公用消息 和 聊天消息
if msgType > 0 {
android.Ups.Options.All.Channel = "yuanmeng_push"
if msgType == int(InstantMsg) || msgType == int(UserAccountMsg) {
//聊天和个人账户
android.Ups.Options.All.Channel = "yuanmeng_push_im"
}
//小米
ximiChannelId := MessageType(msgType).GetXiaoMiChannelId()
android.Ups.Options.Xm = map[string]interface{}{
"/extra.channel_id": ximiChannelId,
"notifyType": -1,
}
//华为
huaweiChannelId, huaweiCategory, importance := MessageType(msgType).GetHuaweiInfo()
android.Ups.Options.Hw = map[string]interface{}{
"/message/android/category": huaweiCategory,
"/message/android/notification/default_sound": true,
"/message/android/notification/channel_id": huaweiChannelId,
"/message/android/notification/visibility": "PUBLIC", //最新接口已没有此参数
"/message/android/notification/importance": importance,
}
//荣耀
honorImportance := MessageType(msgType).GetHonorImportance()
android.Ups.Options.Ho = map[string]interface{}{
"/android/notification/importance": honorImportance,
}
//vivo
vvClassification := MessageType(msgType).GetViVoClassification()
vvCategory := MessageType(msgType).GetViVoCategory()
android.Ups.Options.Vv = map[string]interface{}{
"/classification": vvClassification,
"/notifyType": 4,
"/category": vvCategory,
}
// oppo
oppoChannelId := MessageType(msgType).GetOPPOChannelId()
android.Ups.Options.Op = map[string]interface{}{
"/channel_id": oppoChannelId,
}
}
pushChannel = &models.PushChannel{
Android: android,
IOS: ios,
}
// harmony 厂商通知 配置
if m.AppConfig != nil && m.AppConfig.Harmony != nil {
harmony := &models.HarmonyChannel{}
harmony.Notification = &models.HarmonyNotification{
Title: payload.Title,
Body: payload.Content,
Category: "",
ClickType: "want",
Payload: "",
NotifyId: uint(time.Now().Unix()),
}
wantData := &models.WantData{
DeviceId: "",
BundleName: m.AppConfig.Harmony.BundleName,
AbilityName: m.AppConfig.Harmony.AbilityName,
Action: m.AppConfig.Harmony.Action,
Uri: "",
Parameters: nil,
}
//parameters中添加"gttask":""参数后,个推会自动在 [want] 里拼接 taskid 和 actionid,app 端接收到参数可以用于上报点击埋点
param := make(map[string]interface{})
param["gttask"] = ""
param["data"] = payload
wantData.Parameters = param
b, _ := json.Marshal(wantData)
harmony.Notification.Want = string(b)
//消息分类
harmony.Notification.Category = MessageType(msgType).GetHarmonyCategory()
pushChannel.Harmony = harmony
}
return
}
// getIntent 返回android的intent地址
func getIntent(url string) string {
if url == "" {
return ""
}
intent := fmt.Sprintf("intent:#Intent;launchFlags=0x4000000;component=com.yuanmengzhiyuan.ei8z.yuanmeng_app/.module.appHome.MainActivity;S.nextPage=%s;end", url)
return intent
}
func getRandString() string {
return time.Now().Format("2006-01-02-15-04-05")
}
func getGroupName() string {
return fmt.Sprintf("ymzy_%d", time.Now().Year())
}
// getSplitCid return cut []string
func getSplitCid(cid []string, p, limit int) []string {
list := make([]string, 0)
offset := (p - 1) * limit
count := len(cid)
for i := offset; i < offset+limit && i < count; i++ {
list = append(list, cid[i])
}
return list
}
// getPageCount return pageCount
func getPageCount(limit, count int) (pageCount int) {
if count > 0 && limit > 0 {
if count%limit == 0 {
pageCount = count / limit
} else {
pageCount = count/limit + 1
}
}
return pageCount
}