main.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package main
  2. import (
  3. "encoding/base64"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "strings"
  8. )
  9. const (
  10. push_url = "https://bjapi.push.jiguang.cn/v3/push"
  11. appid = "5efa1257867cf5d77d007ce6"
  12. appSecret = "d7a4e79978cd73cd61a5c7f7"
  13. )
  14. //
  15. func main() {
  16. bodystr := `{
  17. "platform": "all",
  18. "audience": {
  19. "registration_id": [
  20. "140fe1da9ea179bd470"
  21. ]
  22. },
  23. "notification": {
  24. "android": {
  25. "alert": "剑鱼推送消息,最新的订阅内容",
  26. "title": "剑鱼招标订阅",
  27. "builder_id": 1,
  28. "extras": {
  29. "newsid": 321
  30. }
  31. },
  32. "ios": {
  33. "alert": "推送消息",
  34. "sound": "default",
  35. "badge": "+1",
  36. "extras": {
  37. "newsid": 321
  38. }
  39. }
  40. },
  41. "message": {
  42. "msg_content": "Hi,JPush",
  43. "content_type": "text",
  44. "title": "msg",
  45. "extras": {
  46. "key": "value"
  47. }
  48. },
  49. "options": {
  50. "time_to_live": 60,
  51. "apns_production": false,
  52. "apns_collapse_id":"jiguang_test_201706011100"
  53. }
  54. }`
  55. req, _ := http.NewRequest("POST", push_url, strings.NewReader(bodystr))
  56. req.Header.Add("Content-Type", "application/json")
  57. req.Header.Add("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte(appid+":"+appSecret)))
  58. resp, err := http.DefaultClient.Do(req)
  59. if err != nil {
  60. fmt.Println(err.Error())
  61. } else {
  62. bs, _ := ioutil.ReadAll(resp.Body)
  63. resp.Body.Close()
  64. fmt.Println(string(bs))
  65. }
  66. }