作者 yangfu

推送服务 增加透传模板

@@ -13,4 +13,5 @@ const ( @@ -13,4 +13,5 @@ const (
13 */ 13 */
14 const ( 14 const (
15 SystemNotification = iota + 1 //系统消息 15 SystemNotification = iota + 1 //系统消息
  16 + Transmission
16 ) 17 )
@@ -154,6 +154,14 @@ func (notify *GetuiNotification) Message(method string) interface{} { @@ -154,6 +154,14 @@ func (notify *GetuiNotification) Message(method string) interface{} {
154 } 154 }
155 m = t 155 m = t
156 break 156 break
  157 + case push.Transmission:
  158 + t := NewTransmissionTemplate(notify.Options)
  159 + if method == saveListBody {
  160 + t.ClientId = ""
  161 + t.RequestId = ""
  162 + }
  163 + m = t
  164 + break
157 default: 165 default:
158 m = NewNotificationTemplate(notify.Options) 166 m = NewNotificationTemplate(notify.Options)
159 break 167 break
@@ -2,6 +2,7 @@ package getui @@ -2,6 +2,7 @@ package getui
2 2
3 import ( 3 import (
4 "openapi/internal/push" 4 "openapi/internal/push"
  5 + "openapi/internal/utils"
5 "testing" 6 "testing"
6 ) 7 )
7 8
@@ -9,7 +10,7 @@ func TestGetui(t *testing.T) { @@ -9,7 +10,7 @@ func TestGetui(t *testing.T) {
9 var param = make(map[string]interface{}) 10 var param = make(map[string]interface{})
10 param["A"] = "A1" 11 param["A"] = "A1"
11 param["B"] = 2 12 param["B"] = 2
12 - param["C"] = struct{ Id int }{Id: 10} 13 + param["transData"] = struct{ Id int }{Id: 10}
13 notification := &GetuiNotification{} 14 notification := &GetuiNotification{}
14 err := notification.Init( 15 err := notification.Init(
15 push.DebugModule(true), 16 push.DebugModule(true),
@@ -24,11 +25,48 @@ func TestGetui(t *testing.T) { @@ -24,11 +25,48 @@ func TestGetui(t *testing.T) {
24 //push.PushType(push.PushToList), 25 //push.PushType(push.PushToList),
25 //push.ClientIds([]string{"b5fff5f6b0af551da5f381fa47991828"}), 26 //push.ClientIds([]string{"b5fff5f6b0af551da5f381fa47991828"}),
26 27
27 - push.MsgType(1), 28 + push.MsgType(push.Transmission), //push.SystemNotification
28 push.Title("测试 hello"), 29 push.Title("测试 hello"),
29 push.Content("hello content"), 30 push.Content("hello content"),
30 31
31 - push.TransmissionContent("扩展1111"), 32 + push.TransmissionContent(utils.JsonAssertString(param["transData"])),
  33 + push.Extra(param),
  34 + )
  35 + if err != nil {
  36 + t.Fatal(err)
  37 + }
  38 + err = notification.Send(param)
  39 + if err != nil {
  40 + t.Fatal(err)
  41 + }
  42 +}
  43 +
  44 +func TestGetuiPrd(t *testing.T) {
  45 + var param = make(map[string]interface{})
  46 + param["A"] = "A1"
  47 + param["B"] = 2
  48 + param["transData"] = struct {
  49 + Id int `json:"id"`
  50 + }{Id: 1}
  51 + notification := &GetuiNotification{}
  52 + err := notification.Init(
  53 + push.DebugModule(true),
  54 +
  55 + push.AppId("WgrbaaStTk7JElrXOCgUg6"),
  56 + push.AppKey("FG5lbqVrHa5rS9NVfxNP7"),
  57 + push.AppMasterSecret("FW3jMNLJrRARYKv2iqA5H5"),
  58 + //单推
  59 + //push.PushType(push.PushToSingle),
  60 + //push.ClientId("502f4fd7ba5df15ac6b3d5c561efd9ca"),
  61 + //群推
  62 + push.PushType(push.PushToList),
  63 + push.ClientIds([]string{"502f4fd7ba5df15ac6b3d5c561efd9ca"}),
  64 +
  65 + push.MsgType(push.Transmission),
  66 + push.Title("hello"),
  67 + push.Content("hello content"),
  68 +
  69 + push.TransmissionContent(utils.JsonAssertString(param["transData"])),
32 push.Extra(param), 70 push.Extra(param),
33 ) 71 )
34 if err != nil { 72 if err != nil {
@@ -5,21 +5,44 @@ import ( @@ -5,21 +5,44 @@ import (
5 "openapi/internal/push" 5 "openapi/internal/push"
6 ) 6 )
7 7
8 -type Template struct {  
9 - ClientId string `json:"cid,omitempty"`  
10 - RequestId string `json:"requestid,omitempty"`  
11 - Message *Message `json:"message"`  
12 -}  
13 -  
14 //1.消息模板 8 //1.消息模板
15 type NotificationTemplate struct { 9 type NotificationTemplate struct {
16 *Template 10 *Template
17 Notification *Notification `json:"notification"` 11 Notification *Notification `json:"notification"`
18 } 12 }
19 13
  14 +//1.新建通知模板
  15 +func NewNotificationTemplate(options *push.Options) *NotificationTemplate {
  16 + return &NotificationTemplate{
  17 + Template: NewTemplate(options),
  18 + Notification: &Notification{
  19 + Style: (&Style{}).SetStyle0(options),
  20 + Transmission: NewTransmission(options),
  21 + },
  22 + }
  23 +}
  24 +
20 //2.透传模板 25 //2.透传模板
21 -type TransmissionTemplate struct{} 26 +type TransmissionTemplate struct {
  27 + *Template
  28 + Transmission *Transmission `json:"transmission"`
  29 + PushInfo *PushInfo `json:"push_info"`
  30 +}
  31 +
  32 +//2.新建透传模板
  33 +func NewTransmissionTemplate(options *push.Options) *TransmissionTemplate {
  34 + return &TransmissionTemplate{
  35 + Template: NewTemplate(options),
  36 + Transmission: NewTransmission(options),
  37 + PushInfo: NewPushInfo(options),
  38 + }
  39 +}
22 40
  41 +type Template struct {
  42 + ClientId string `json:"cid,omitempty"`
  43 + RequestId string `json:"requestid,omitempty"`
  44 + Message *Message `json:"message"`
  45 +}
23 type Notification struct { 46 type Notification struct {
24 Style *Style `json:"style"` 47 Style *Style `json:"style"`
25 *Transmission 48 *Transmission
@@ -29,9 +52,11 @@ type Message struct { @@ -29,9 +52,11 @@ type Message struct {
29 IsOffline bool `json:"is_offline"` 52 IsOffline bool `json:"is_offline"`
30 MsgType string `json:"msgtype"` 53 MsgType string `json:"msgtype"`
31 } 54 }
  55 +
  56 +//透传
32 type Transmission struct { 57 type Transmission struct {
33 - TransmissionType bool `json:"transmission_type"`  
34 - TransmissionContent string `json:"transmission_content,omitempty"` 58 + TransmissionType bool `json:"transmission_type"` //收到消息是否立即启动应用,true为立即启动,false则广播等待启动,默认是否
  59 + TransmissionContent string `json:"transmission_content,omitempty"` //透传内容
35 DurationBegin string `json:"duration_begin,omitempty"` 60 DurationBegin string `json:"duration_begin,omitempty"`
36 DurationEnd string `json:"duration_end,omitempty"` 61 DurationEnd string `json:"duration_end,omitempty"`
37 } 62 }
@@ -47,16 +72,6 @@ func (o *Transmission) SetDuration(begin, end string) { @@ -47,16 +72,6 @@ func (o *Transmission) SetDuration(begin, end string) {
47 o.DurationEnd = end 72 o.DurationEnd = end
48 } 73 }
49 74
50 -//1.新建单推  
51 -func NewNotificationTemplate(options *push.Options) *NotificationTemplate {  
52 - return &NotificationTemplate{  
53 - Template: NewTemplate(options),  
54 - Notification: &Notification{  
55 - Style: (&Style{}).SetStyle0(options),  
56 - Transmission: NewTransmission(options),  
57 - },  
58 - }  
59 -}  
60 func NewTemplate(options *push.Options) *Template { 75 func NewTemplate(options *push.Options) *Template {
61 return &Template{ 76 return &Template{
62 Message: NewMessage(options), 77 Message: NewMessage(options),
@@ -67,10 +82,10 @@ func NewTemplate(options *push.Options) *Template { @@ -67,10 +82,10 @@ func NewTemplate(options *push.Options) *Template {
67 func NewTransmission(options *push.Options) *Transmission { 82 func NewTransmission(options *push.Options) *Transmission {
68 t := &Transmission{} 83 t := &Transmission{}
69 if len(options.TransmissionContent) == 0 { 84 if len(options.TransmissionContent) == 0 {
70 - t.SetTransmissionType(false) 85 + //t.SetTransmissionType(false)
71 return t 86 return t
72 } 87 }
73 - t.SetTransmissionType(true) 88 + t.SetTransmissionType(false)
74 t.SetTransmissionContent(options.TransmissionContent) 89 t.SetTransmissionContent(options.TransmissionContent)
75 return t 90 return t
76 } 91 }
@@ -89,7 +104,8 @@ func resolveMsgType(msgType int) string { @@ -89,7 +104,8 @@ func resolveMsgType(msgType int) string {
89 switch msgType { 104 switch msgType {
90 case push.SystemNotification: 105 case push.SystemNotification:
91 return "notification" 106 return "notification"
92 - 107 + case push.Transmission:
  108 + return "transmission"
93 } 109 }
94 return "notification" 110 return "notification"
95 } 111 }
@@ -105,13 +121,16 @@ type Style struct { @@ -105,13 +121,16 @@ type Style struct {
105 Logo string `json:"logo,omitempty"` //通知的图标名称,包含后缀名(需要在客户端开发时嵌入),如“push.png” 121 Logo string `json:"logo,omitempty"` //通知的图标名称,包含后缀名(需要在客户端开发时嵌入),如“push.png”
106 //IsRing bool `json:"is_ring"` //收到通知是否响铃:true响铃,false不响铃。默认响铃 122 //IsRing bool `json:"is_ring"` //收到通知是否响铃:true响铃,false不响铃。默认响铃
107 //IsVibrate bool `json:"is_vibrate"` //收到通知是否振动:true振动,false不振动。默认振动 123 //IsVibrate bool `json:"is_vibrate"` //收到通知是否振动:true振动,false不振动。默认振动
  124 + NotifyId int `json:"notify_id"` //需要被覆盖的消息已经增加了notifyId字段,用于实现下发消息的覆盖。新的消息使用相同的notifyId下发。
108 } 125 }
109 126
  127 +//设置默认样式 0
110 func (s *Style) SetStyle0(options *push.Options) *Style { 128 func (s *Style) SetStyle0(options *push.Options) *Style {
111 s.Type = 0 129 s.Type = 0
112 s.Title = options.Title 130 s.Title = options.Title
113 s.Text = options.Content 131 s.Text = options.Content
114 s.Logo = "push.png" //TODO:设置Logo地址 132 s.Logo = "push.png" //TODO:设置Logo地址
  133 + s.NotifyId = 1
115 return s 134 return s
116 } 135 }
117 136
@@ -126,9 +145,47 @@ type AuthSignResponse struct { @@ -126,9 +145,47 @@ type AuthSignResponse struct {
126 AuthToken string `json:"auth_token"` 145 AuthToken string `json:"auth_token"`
127 } 146 }
128 147
  148 +//应答结果
129 type Result struct { 149 type Result struct {
130 Result string `json:"result"` 150 Result string `json:"result"`
131 TaskId string `json:"taskid"` 151 TaskId string `json:"taskid"`
132 Status string `json:"status"` 152 Status string `json:"status"`
133 Desc string `json:"desc"` 153 Desc string `json:"desc"`
134 } 154 }
  155 +
  156 +//透传附加的推送信息
  157 +func NewPushInfo(options *push.Options) (v *PushInfo) {
  158 + v = &PushInfo{
  159 + Aps: NewAps(options),
  160 + }
  161 + return
  162 +}
  163 +func NewAps(options *push.Options) (v *Aps) {
  164 + v = &Aps{
  165 + Alert: NewAlert(options),
  166 + AutoBadge: "+1",
  167 + ContentAvailable: 1,
  168 + }
  169 + return
  170 +}
  171 +func NewAlert(options *push.Options) (v *Alert) {
  172 + v = &Alert{
  173 + Title: options.Title,
  174 + Body: options.Content,
  175 + }
  176 + return
  177 +}
  178 +
  179 +type PushInfo struct {
  180 + Aps *Aps `json:"aps"`
  181 +}
  182 +type Aps struct {
  183 + Alert *Alert `json:"alert"`
  184 + AutoBadge string `json:"autoBadge"` //用于计算应用上面未读数字
  185 + ContentAvailable int `json:"content-available"` //推送直接带有透传数据 0:有通知栏消息 1:无通知栏消息
  186 + Payload string `json:"payload,omitempty"`
  187 +}
  188 +type Alert struct {
  189 + Title string `json:"title"`
  190 + Body string `json:"body"`
  191 +}