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

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
}