firebase implementation

This commit is contained in:
2022-11-19 14:57:45 +01:00
committed by Mike Schwörer
parent e53f40866e
commit fb37f94c0a
6 changed files with 271 additions and 21 deletions

View File

@@ -0,0 +1,74 @@
package firebase
import (
"context"
_ "embed"
fb "firebase.google.com/go"
"firebase.google.com/go/messaging"
"github.com/rs/zerolog/log"
"google.golang.org/api/option"
)
//go:embed scnserviceaccountkey.json
var scnserviceaccountkey []byte
type App struct {
app *fb.App
messaging *messaging.Client
}
func NewFirebaseApp() App {
opt := option.WithCredentialsJSON(scnserviceaccountkey)
app, err := fb.NewApp(context.Background(), nil, opt)
if err != nil {
log.Error().Err(err).Msg("failed to init firebase app")
}
msg, err := app.Messaging(context.Background())
if err != nil {
log.Error().Err(err).Msg("failed to init messaging client")
}
log.Info().Msg("Initialized Firebase")
return App{
app: app,
messaging: msg,
}
}
type Notification struct {
Id string
Token string
Platform string
Title string
Body string
Priority int
}
func (fb App) SendNotification(ctx context.Context, notification Notification) (string, error) {
n := messaging.Message{
Data: map[string]string{"scn_msg_id": notification.Id},
Notification: &messaging.Notification{
Title: notification.Title,
Body: notification.Body,
},
Android: nil,
APNS: nil,
Webpush: nil,
FCMOptions: nil,
Token: notification.Token,
Topic: "",
Condition: "",
}
if notification.Platform == "ios" {
n.APNS = nil
}
if notification.Platform == "android" {
n.Android = nil
}
res, err := fb.messaging.Send(ctx, &n)
if err != nil {
log.Error().Err(err).Msg("failed to send push")
}
return res, err
}

View File

@@ -0,0 +1,12 @@
{
"type": "service_account",
"project_id": "simplecloudnotifier-ea7ef",
"private_key_id": "5bfab19fca25034e87c5b3bd1a4334499d2d1f85",
"private_key": "-----BEGIN PRIVATE KEY-----\nMIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQD2NWOQDcalRdkp\nHtQHABLlu3GMBQBJrGiCxzOZhi/lLwrw2MJEmg1VFz6TVkX2z3SCzXCPOgGriM70\nuWCNLyZQvUng7u6/WH9hlpCg0vJpkw6BvOBt1zYu3g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JKXT7On696h5C6DBzKPAqUf3Yh88mqvMLDHkQnE6daLv7vykxAoGAOPmA\ndDx1NO48E1+OIwgRyqv9PUZmDB3Qit5L4biN6lvgJqlJOV+PeRokZ2wOKLLZVkeF\nh2BTrhFgXDJfESEz6rT0eljsTHVIUK/E8On5Ttd5z1SrYUII3NfpAhP9mWaVr6tC\nxX1hMYWAr+Ho9PM23iFoL5U+IdqSLvqdkPVYfPcCgYB1ANKNYPIJNx/wLxYWNS0r\nI98HwKfv2TxxE/l+2459NMMHY5wlpFl7MNoeK2SdY+ghWPlxC6u5Nxpnk+bZ8TJe\np7U2nY0SQDLCmPgGWs3KBb/zR49X2b7JS3CXXqQSrLxBe2phZg6kE5nB6NPUDc/i\n6WG8tG20rCfgwlXeXl0+Ow==\n-----END PRIVATE KEY-----\n",
"client_email": "firebase-adminsdk-42grv@simplecloudnotifier-ea7ef.iam.gserviceaccount.com",
"client_id": "109837766844812362714",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://oauth2.googleapis.com/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/firebase-adminsdk-42grv%40simplecloudnotifier-ea7ef.iam.gserviceaccount.com"
}