fcm_test.go 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package server
  2. import (
  3. "context"
  4. "log"
  5. "testing"
  6. firebase "firebase.google.com/go"
  7. "firebase.google.com/go/messaging"
  8. "google.golang.org/api/option"
  9. )
  10. func TestFirebaseMessageServer_Send(t *testing.T) {
  11. ctx := context.Background()
  12. conf := &firebase.Config{ServiceAccountID: androidFirebaseServiceAccountID}
  13. app, err := firebase.NewApp(ctx, conf,
  14. option.WithCredentialsJSON([]byte(androidFirebaseCredentials)))
  15. if err != nil {
  16. log.Fatalln(err)
  17. }
  18. //token := "fDfRE2raSkSfroIw47qSEV:APA91bF4LORpS8ZWo-i8i1DQ8SZ8C-BEdFWBdGQGDW-PwzQUivoNf75_NpTrFNLFlAqdHd4mdauaI9aZWhFs4Pou2iySmuI6Ct0sHs5Bc9LrnGMK9f5YEBA"
  19. token := "cl7mDFUw5URmvG9Jio2p7L:APA91bH4GIRM1IJNDn7_tYw7q4ADVdzQKpnaPNqxXw6PZE-H9H82sGkF74-KSz7moydB6ykgHucrXNjsSopmyFCi3M4S-SVWZA_fGJa6Q0WO6V_CY_gtDhi2WHGobM84Pm0aZ0AHb2XB"
  20. clt, err := app.Messaging(ctx)
  21. if err != nil {
  22. panic(err)
  23. }
  24. jsonStr := "hello word"
  25. title := "测试消息"
  26. body := "测试消息内容,无需关注"
  27. //title:= "Call Notification"
  28. //body:= "click to view"
  29. //videoCallMessage := &NatsPubVideoCallMessage{
  30. // AccountId: "954d38fc-f4a4-445f-88e3-7be83a51a3c8",
  31. // Dial: 1,
  32. // Receiver: "c2686cac-8ec8-4b26-8e87-50b7d63a7d36",
  33. // Sender: "954d38fc-f4a4-445f-88e3-7be83a51a3c8",
  34. //}
  35. //
  36. //message := NatsPubMessage[NatsPubVideoCallMessage]{
  37. // RequestId: "41a83fe2-6281-4f72-bb80-067fb5e19b1c",
  38. // Type: MessageTypeVideoCall,
  39. // Content: videoCallMessage,
  40. //}
  41. //jsonBytes, err := json.Marshal(message)
  42. //if err != nil {
  43. // fmt.Println("Error marshaling JSON:", err)
  44. // return
  45. //}
  46. //jsonStr := string(jsonBytes)
  47. msg := &messaging.Message{
  48. Token: token,
  49. Data: map[string]string{
  50. "message": jsonStr,
  51. },
  52. Android: &messaging.AndroidConfig{
  53. Notification: &messaging.AndroidNotification{
  54. Sound: "default",
  55. Priority: messaging.PriorityHigh,
  56. },
  57. },
  58. APNS: &messaging.APNSConfig{
  59. Payload: &messaging.APNSPayload{
  60. Aps: &messaging.Aps{
  61. Sound: "default",
  62. },
  63. },
  64. },
  65. Notification: &messaging.Notification{
  66. Title: title,
  67. Body: body,
  68. },
  69. }
  70. r, err := clt.Send(ctx, msg)
  71. if err != nil {
  72. log.Fatalln(err)
  73. } else {
  74. log.Println(r)
  75. }
  76. }