notification.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. package mi
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/url"
  6. "strconv"
  7. "time"
  8. )
  9. // XMMessage define reference struct http://dev.xiaomi.com/doc/?p=533
  10. type XMMessage struct {
  11. Payload string // 消息的内容。
  12. RestrictedPackageName string // App的包名。备注:V2版本支持一个包名,V3版本支持多包名(中间用逗号分割)。
  13. PassThrough int // pass_through的值可以为: 0 表示通知栏消息1 表示透传消息
  14. NotifyType int // 通知方式
  15. Title string // 通知栏展示的通知的标题。
  16. Description string // 通知栏展示的通知的描述。
  17. TaskID string // 上报数据使用
  18. xmuv url.Values // 含有本条消息所有属性的数组
  19. }
  20. func (xm *XMMessage) buildXMPostParam() {
  21. xmuv := url.Values{}
  22. xmuv.Set("payload", xm.Payload)
  23. xmuv.Set("restricted_package_name", xm.RestrictedPackageName)
  24. xmuv.Set("pass_through", strconv.Itoa(xm.PassThrough))
  25. xmuv.Set("title", xm.Title)
  26. xmuv.Set("description", xm.Description)
  27. xmuv.Set("notify_type", strconv.Itoa(xm.NotifyType))
  28. xmuv.Set("extra.task_id", xm.TaskID)
  29. xmuv.Set("extra.jobkey", xm.TaskID)
  30. xmuv.Set("extra.callback", CallbackURL)
  31. xmuv.Set("extra.callback.type", "1") // 第三方所需要的回执类型。1:送达回执,2:点击回执,3:送达和点击回执,默认值为3。
  32. xm.xmuv = xmuv
  33. }
  34. // SetNotifyID 可选项
  35. // 默认情况下,通知栏只显示一条推送消息。如果通知栏要显示多条推送消息,需要针对不同的消息设置不同的notify_id(相同notify_id的通知栏消息会覆盖之前的)。
  36. // notify_id 0-4 同一个notifyId在通知栏只会保留一条
  37. func (xm *XMMessage) SetNotifyID(notifyID string) {
  38. if xm.xmuv == nil {
  39. xm.buildXMPostParam()
  40. }
  41. xm.xmuv.Set("notify_id", notifyID)
  42. }
  43. // SetNotifyType sound / vibration / led light
  44. func (xm *XMMessage) SetNotifyType(typ int) {
  45. if xm.xmuv == nil {
  46. xm.buildXMPostParam()
  47. }
  48. xm.xmuv.Set("notify_type", strconv.Itoa(typ))
  49. }
  50. // SetTimeToLive 可选项
  51. // 如果用户离线,设置消息在服务器保存的时间,单位:ms。服务器默认最长保留两周。
  52. // time_to_live 可选项,当用户离线是,消息保留时间,默认两周,单位ms
  53. func (xm *XMMessage) SetTimeToLive(expire int64) {
  54. if xm.xmuv == nil {
  55. xm.buildXMPostParam()
  56. }
  57. timeToLive := (expire - time.Now().Unix()) * 1000
  58. xm.xmuv.Set("time_to_live", fmt.Sprintf("%d", timeToLive))
  59. }
  60. // SetTimeToSend 可选项
  61. // 定时发送消息。用自1970年1月1日以来00:00:00.0 UTC时间表示(以毫秒为单位的时间)。注:仅支持七天内的定时消息。
  62. func (xm *XMMessage) SetTimeToSend(timeToSend int64) {
  63. if xm.xmuv == nil {
  64. xm.buildXMPostParam()
  65. }
  66. xm.xmuv.Set("time_to_send", fmt.Sprintf("%d", timeToSend))
  67. }
  68. // SetUserAccount 根据user_account,发送消息给设置了该user_account的所有设备。可以提供多个user_account,user_account之间用“,”分割。参数仅适用于“/message/user_account”HTTP API。
  69. func (xm *XMMessage) SetUserAccount(UserAccount string) {
  70. if xm.xmuv == nil {
  71. xm.buildXMPostParam()
  72. }
  73. xm.xmuv.Set("user_account", UserAccount)
  74. }
  75. // SetUserAccounts 针对不同的userAccount推送不同的消息
  76. // 根据user_accounts,发送消息给设置了该user_account的所有设备。可以提供多个user_account,user_account之间用“,”分割。
  77. func (xm *XMMessage) SetUserAccounts(UserAccount string) {
  78. if xm.xmuv == nil {
  79. xm.buildXMPostParam()
  80. }
  81. xm.xmuv.Set("user_accounts", UserAccount)
  82. }
  83. // SetRegID 根据registration_id,发送消息到指定设备上。可以提供多个registration_id,发送给一组设备,不同的registration_id之间用“,”分割。
  84. func (xm *XMMessage) SetRegID(deviceToken string) {
  85. if xm.xmuv == nil {
  86. xm.buildXMPostParam()
  87. }
  88. xm.xmuv.Set("registration_id", deviceToken)
  89. }
  90. // SetTopic 根据topic,发送消息给订阅了该topic的所有设备。参数仅适用于“/message/topic”HTTP API。
  91. func (xm *XMMessage) SetTopic(UserAccount string) {
  92. if xm.xmuv == nil {
  93. xm.buildXMPostParam()
  94. }
  95. xm.xmuv.Set("topic", UserAccount)
  96. }
  97. // SetCallbackParam 把应用标识传过去,这样方便区分应用
  98. func (xm *XMMessage) SetCallbackParam(p string) {
  99. if xm.xmuv == nil {
  100. xm.buildXMPostParam()
  101. }
  102. xm.xmuv.Set("extra.callback.param", p) // 可选字段。第三方自定义回执参数,最大长度64个字节(这里用来存应用ID)
  103. }
  104. // Response push result.
  105. type Response struct {
  106. Result string `json:"result,omitempty"` //“result”: string,”ok” 表示成功, “error” 表示失败。
  107. Reason string `json:"reason,omitempty"` //reason: string,如果失败,reason失败原因详情。
  108. Code int `json:"code,omitempty"` //“code”: integer,0表示成功,非0表示失败。
  109. Data Data `json:"data,omitempty"` //“data”: string,本身就是一个json字符串(其中id字段的值就是消息的Id)。
  110. Description string `json:"description,omitempty"` //“description”: string, 对发送消息失败原因的解释。
  111. Info string `json:"info,omitempty"` //“info”: string,详细信息。
  112. TraceID string `json:"trace_id,omitempty"` // trace id for xiaomi
  113. }
  114. // Data response data.
  115. type Data struct {
  116. ID string `json:"id,omitempty"`
  117. List []string `json:"list,omitempty"` // for feedback
  118. Data json.RawMessage `json:"data,omitempty"` // for status
  119. }
  120. // UninstalledResponse .
  121. type UninstalledResponse struct {
  122. Code int `json:"errorCode,omitempty"`
  123. Reason string `json:"reason,omitempty"`
  124. Result []string `json:"result,omitempty"`
  125. Data []string
  126. }
  127. // UninstalledData .
  128. type UninstalledData struct {
  129. Token string `json:"regId"`
  130. Ts int64 `json:"ts"`
  131. // Alias []string `json:"alias"` // 用不上
  132. }
  133. // RegidCallback regid callback
  134. type RegidCallback struct {
  135. AppID string `json:"app_id"`
  136. AppVer string `json:"app_version"`
  137. AppPkg string `json:"app_pkg"`
  138. AppSecret string `json:"app_secret"`
  139. Regid string `json:"regid"`
  140. }
  141. // Callback 推送回执(回调)
  142. type Callback struct {
  143. Param string `json:"param"` // 开发者上传的自定义参数值。
  144. BarStatus string `json:"barStatus"` // 消息送达时通知栏的状态。Enable:为用户允许此app展示通知栏消息, Disable:为通知栏消息已关闭, Unknown:通知栏状态未知。
  145. Type int `json:"type"` // callback类型
  146. Targets string `json:"targets"` // 一批alias或者regId列表,之间是用逗号分割
  147. Jobkey string `json:"jobkey"`
  148. }