message.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. package huawei
  2. import (
  3. "encoding/json"
  4. )
  5. const (
  6. // MsgTypePassthrough 消息类型:透传
  7. MsgTypePassthrough = 1
  8. // MsgTypeNotification 消息类型:通知栏消息
  9. MsgTypeNotification = 3
  10. // ActionTypeCustom 动作类型:自定义
  11. ActionTypeCustom = 1
  12. // ActionTypeURL 动作类型:打开URL
  13. ActionTypeURL = 2
  14. // ActionTypeAPP 动作类型:打开APP
  15. ActionTypeAPP = 3
  16. // CallbackTokenUninstalled 应用被卸载了
  17. CallbackTokenUninstalled = 2
  18. // CallbackTokenNotApply 终端安装了该应用,但从未打开过,未申请token,所以不能展示
  19. CallbackTokenNotApply = 5
  20. // CallbackTokenInactive 非活跃设备,消息丢弃
  21. CallbackTokenInactive = 10
  22. )
  23. // Response push response.
  24. type Response struct {
  25. Code string `json:"code"`
  26. Msg string `json:"msg"`
  27. Err string `json:"error"`
  28. RequestID string `json:"requestId"`
  29. }
  30. // InvalidTokenResponse invalid tokens info in the push response.
  31. type InvalidTokenResponse struct {
  32. Success int `json:"success"`
  33. Failure int `json:"failure"`
  34. IllegalTokens []string `json:"illegal_tokens"`
  35. }
  36. // Message request message.
  37. type Message struct {
  38. Hps Hps `json:"hps"`
  39. }
  40. // Hps .
  41. type Hps struct {
  42. Msg Msg `json:"msg"`
  43. Ext Ext `json:"ext"`
  44. }
  45. // Msg .
  46. type Msg struct {
  47. Type int `json:"type"`
  48. Body Body `json:"body"`
  49. Action Action `json:"action"`
  50. }
  51. // Body .
  52. type Body struct {
  53. Content string `json:"content"`
  54. Title string `json:"title"`
  55. }
  56. // Action .
  57. type Action struct {
  58. Type int `json:"type"`
  59. Param Param `json:"param"`
  60. }
  61. // Param .
  62. type Param struct {
  63. Intent string `json:"intent"`
  64. AppPkgName string `json:"appPkgName"`
  65. }
  66. // Ext .
  67. type Ext struct {
  68. BiTag string `json:"biTag"`
  69. Icon string `json:"icon"`
  70. Customize []map[string]string `json:"customize"`
  71. }
  72. // Callback 华为推送回执(回调)
  73. type Callback struct {
  74. Statuses []*CallbackItem `json:"statuses"`
  75. }
  76. // CallbackItem http://developer.huawei.com/consumer/cn/service/hms/catalog/huaweipush_agent.html?page=hmssdk_huaweipush_devguide_server_agent#3.3 消息回执
  77. type CallbackItem struct {
  78. BiTag string `json:"biTag"`
  79. AppID string `json:"appid"`
  80. Token string `json:"token"`
  81. Status int `json:"status"`
  82. Timestamp int64 `json:"timestamp"`
  83. }
  84. // NewMessage get message.
  85. func NewMessage() *Message {
  86. return &Message{
  87. Hps: Hps{
  88. Msg: Msg{
  89. Type: MsgTypeNotification, //1 透传异步消息, 3 系统通知栏异步消息 注意:2和4以后为保留后续扩展使用
  90. Body: Body{
  91. Content: "",
  92. Title: "",
  93. },
  94. Action: Action{
  95. Type: ActionTypeAPP, //1 自定义行为, 2 打开URL ,3 打开App
  96. Param: Param{},
  97. },
  98. },
  99. Ext: Ext{ //扩展信息,含BI消息统计,特定展示风格,消息折叠。
  100. BiTag: "Trump", // 设置消息标签,如果带了这个标签,会在回执中推送给CP用于检测某种类型消息的到达率和状态
  101. },
  102. },
  103. }
  104. }
  105. // SetContent sets content.
  106. func (m *Message) SetContent(content string) *Message {
  107. m.Hps.Msg.Body.Content = content
  108. return m
  109. }
  110. // SetTitle sets title.
  111. func (m *Message) SetTitle(title string) *Message {
  112. m.Hps.Msg.Body.Title = title
  113. return m
  114. }
  115. // SetMsgType sets title.
  116. func (m *Message) SetMsgType(typ int) *Message {
  117. m.Hps.Msg.Type = typ
  118. return m
  119. }
  120. // SetIntent sets intent.
  121. func (m *Message) SetIntent(intent string) *Message {
  122. m.Hps.Msg.Action.Param.Intent = intent
  123. return m
  124. }
  125. // SetPkg sets app package name.
  126. func (m *Message) SetPkg(pkg string) *Message {
  127. m.Hps.Msg.Action.Param.AppPkgName = pkg
  128. return m
  129. }
  130. // SetCustomize set ext info.
  131. func (m *Message) SetCustomize(key, val string) *Message {
  132. mp := map[string]string{key: val}
  133. m.Hps.Ext.Customize = append(m.Hps.Ext.Customize, mp)
  134. return m
  135. }
  136. // SetBiTag set biTag.
  137. func (m *Message) SetBiTag(tag string) *Message {
  138. m.Hps.Ext.BiTag = tag
  139. return m
  140. }
  141. // SetIcon sets icon.
  142. func (m *Message) SetIcon(url string) *Message {
  143. m.Hps.Ext.Icon = url
  144. return m
  145. }
  146. // JSON encode the message.
  147. func (m *Message) JSON() (res string, err error) {
  148. bytes, err := json.Marshal(m)
  149. if err != nil {
  150. return
  151. }
  152. res = string(bytes)
  153. return
  154. }