|
|
- package Service
-
- import (
- "context"
- "os"
-
- firebase "firebase.google.com/go"
- "firebase.google.com/go/messaging"
- "github.com/joho/godotenv"
-
- "google.golang.org/api/option"
- )
-
- var (
- ctx context.Context
- fcmClient *messaging.Client
- )
-
- func init() {
- var (
- fireBaseAuthKey string
- opts []option.ClientOption
- app *firebase.App
- err error
- )
-
- err = godotenv.Load()
- if err != nil {
- panic(err)
- }
-
- fireBaseAuthKey = os.Getenv("FIREBASE_AUTH_KEY")
-
- opts = []option.ClientOption{
- option.WithCredentialsFile(fireBaseAuthKey),
- }
-
- ctx = context.TODO()
-
- app, err = firebase.NewApp(ctx, nil, opts...)
- if err != nil {
- panic(err)
- }
-
- fcmClient, err = app.Messaging(ctx)
- if err != nil {
- panic(err)
- }
- }
-
- func SendNotification(tokens []string, title string, data map[string]string) error {
- var (
- message messaging.MulticastMessage
- err error
- )
-
- message = messaging.MulticastMessage{
- Notification: &messaging.Notification{
- Title: title,
- },
- Data: data,
- Tokens: tokens,
- }
-
- _, err = fcmClient.SendMulticast(context.TODO(), &message)
-
- return err
- }
|