client_test.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package fcm
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "strings"
  6. "testing"
  7. "time"
  8. "unicode"
  9. . "github.com/smartystreets/goconvey/convey"
  10. )
  11. const apiKey = "AIzaSyBtMplqJkuTIDyIx-CM74MoPHbxHCBcYYQ"
  12. func TestPush(t *testing.T) {
  13. Convey("test jpush", t, func() {
  14. data := map[string]string{
  15. "task_id": "123456",
  16. // "scheme": model.Scheme(model.LinkTypeVideo, "123", model.PlatformAndroid, 390000),
  17. "scheme": "bilibili://video/123",
  18. }
  19. client := NewClient(apiKey, 5*time.Second)
  20. message := &Message{
  21. // DryRun: true, // 如果是 true,消息不会下发给用户,用于测试
  22. Data: data,
  23. RegistrationIDs: []string{"fpICefK-jfE:APA91bHjZTxe503tpFoFMmXXX9LAiMmg7OwgTPYmTb8Ox-yF88umTQnmTQUGbALplxqre7R6v3d0-vSK5MyT4jFtSqklbY1GIaM4d8uZ0wJlwWrRWdBDeOJ4rlpvamd3aGyBlHKAH18N"},
  24. Priority: PriorityHigh,
  25. DelayWhileIdle: true,
  26. Notification: Notification{
  27. Title: "Hello",
  28. Body: "World",
  29. ClickAction: "com.bilibili.app.in.com.bilibili.push.FCM_MESSAGE",
  30. },
  31. CollapseKey: strings.TrimFunc("t123456", func(r rune) bool {
  32. return !unicode.IsNumber(r)
  33. }), // 值转成 int 传到客户端
  34. TimeToLive: int(time.Hour.Seconds()),
  35. Android: Android{Priority: PriorityHigh},
  36. }
  37. response, err := client.Send(message)
  38. msgb, _ := json.Marshal(message)
  39. fmt.Printf("msg(%s)", msgb)
  40. So(err, ShouldNotBeNil)
  41. if err != nil {
  42. t.Errorf("fcm send response(%+v) error(%v)", response, err)
  43. } else {
  44. fmt.Println("Status Code :", response.StatusCode)
  45. fmt.Println("Success :", response.Success)
  46. fmt.Println("Fail :", response.Fail)
  47. fmt.Println("Canonical_ids :", response.CanonicalIDs)
  48. fmt.Println("Topic MsgId :", response.MsgID)
  49. }
  50. })
  51. }
  52. func Test_ClientFaild(t *testing.T) {
  53. Convey("test jpush", t, func() {
  54. client := NewClient(apiKey, 5*time.Second)
  55. err := client.Failed(&Response{})
  56. So(err, ShouldBeNil)
  57. r := &Response{RetryAfter: "3m"}
  58. _, err = r.GetRetryAfterTime()
  59. So(err, ShouldBeNil)
  60. })
  61. }