-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
41 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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" | ||
|
@@ -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 | ||
} |