Skip to content

Commit

Permalink
Add initial welcome notification
Browse files Browse the repository at this point in the history
  • Loading branch information
TomRomeo committed Jan 20, 2024
1 parent 1822eeb commit 2955433
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions backend/services/notifications/handler/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ package handler

import (
"context"
"encoding/json"
"os"

"github.com/SherClockHolmes/webpush-go"
"github.com/kioku-project/kioku/pkg/model"
pbCommon "github.com/kioku-project/kioku/pkg/proto"
pbSrs "github.com/kioku-project/kioku/services/srs/proto"
Expand Down Expand Up @@ -32,6 +35,44 @@ func (e *Notifications) Enroll(ctx context.Context, req *pb.PushSubscriptionRequ
if err := e.store.CreatePushSubscription(ctx, subscription); err != nil {
return err
}
privateKey, success := os.LookupEnv("VAPID_PRIVATE_KEY")
if !success {
logger.Fatal("VAPID_PRIVATE_KEY environment variable not set")
}
publicKey, success := os.LookupEnv("VAPID_PUBLIC_KEY")
if !success {
logger.Fatal("VAPID_PUBLIC_KEY not set")
}
s := &webpush.Subscription{
Endpoint: subscription.Endpoint,
Keys: webpush.Keys{
P256dh: subscription.P256DH,
Auth: subscription.Auth,
},
}
notification := &model.PushNotification{
Title: "Welcome to Kioku!",
Options: model.PushNotificationOptions{
Body: "You will now receive reminders so you don't forget your cards'!",
Actions: []map[string]string{},
Vibrate: []int{200, 100, 200},
Tag: "Kioku",
},
}
jsonNotification, err := json.Marshal(notification)
if err != nil {
return err
}
resp, err := webpush.SendNotification(jsonNotification, s, &webpush.Options{
Subscriber: "[email protected]",
VAPIDPublicKey: publicKey,
VAPIDPrivateKey: privateKey,
TTL: 30,
})
if err != nil {
return err
}
defer resp.Body.Close()
rsp.Success = true
return nil
}

0 comments on commit 2955433

Please sign in to comment.