payload.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package jpush
  2. import (
  3. "encoding/json"
  4. )
  5. // Payload .
  6. type Payload struct {
  7. Platform interface{} `json:"platform"`
  8. Audience interface{} `json:"audience"`
  9. Notification interface{} `json:"notification,omitempty"`
  10. Message interface{} `json:"message,omitempty"`
  11. Options *Option `json:"options,omitempty"`
  12. Callback *CallbackReq `json:"callback,omitempty"`
  13. }
  14. // NewPayload .
  15. func NewPayload() *Payload {
  16. return &Payload{
  17. Options: &Option{},
  18. }
  19. }
  20. // SetPlatform .
  21. func (p *Payload) SetPlatform(plat *Platform) {
  22. p.Platform = plat.OS
  23. }
  24. // SetAudience .
  25. func (p *Payload) SetAudience(ad *Audience) {
  26. p.Audience = ad.Object
  27. }
  28. // SetOptions .
  29. func (p *Payload) SetOptions(o *Option) {
  30. p.Options = o
  31. }
  32. // SetMessage .
  33. func (p *Payload) SetMessage(m *Message) {
  34. p.Message = m
  35. }
  36. // SetNotice .
  37. func (p *Payload) SetNotice(notice *Notice) {
  38. p.Notification = notice
  39. }
  40. // SetCallbackReq .
  41. func (p *Payload) SetCallbackReq(cb *CallbackReq) {
  42. p.Callback = cb
  43. }
  44. // ToBytes .
  45. func (p *Payload) ToBytes() ([]byte, error) {
  46. content, err := json.Marshal(p)
  47. if err != nil {
  48. return nil, err
  49. }
  50. return content, nil
  51. }