Encrypted messaging app
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.1 KiB

  1. package Service
  2. import (
  3. "context"
  4. "os"
  5. firebase "firebase.google.com/go"
  6. "firebase.google.com/go/messaging"
  7. "github.com/joho/godotenv"
  8. "google.golang.org/api/option"
  9. )
  10. var (
  11. ctx context.Context
  12. fcmClient *messaging.Client
  13. )
  14. func init() {
  15. var (
  16. fireBaseAuthKey string
  17. opts []option.ClientOption
  18. app *firebase.App
  19. err error
  20. )
  21. err = godotenv.Load()
  22. if err != nil {
  23. panic(err)
  24. }
  25. fireBaseAuthKey = os.Getenv("FIREBASE_AUTH_KEY")
  26. opts = []option.ClientOption{
  27. option.WithCredentialsFile(fireBaseAuthKey),
  28. }
  29. ctx = context.TODO()
  30. app, err = firebase.NewApp(ctx, nil, opts...)
  31. if err != nil {
  32. panic(err)
  33. }
  34. fcmClient, err = app.Messaging(ctx)
  35. if err != nil {
  36. panic(err)
  37. }
  38. }
  39. func SendNotification(tokens []string, title string, data map[string]string) error {
  40. var (
  41. message messaging.MulticastMessage
  42. err error
  43. )
  44. message = messaging.MulticastMessage{
  45. Notification: &messaging.Notification{
  46. Title: title,
  47. },
  48. Data: data,
  49. Tokens: tokens,
  50. }
  51. _, err = fcmClient.SendMulticast(context.TODO(), &message)
  52. return err
  53. }