2019-06-03 09:32:43 +00:00
|
|
|
package arn
|
|
|
|
|
|
|
|
import (
|
2019-11-02 16:56:42 +00:00
|
|
|
"encoding/json"
|
2019-06-03 09:32:43 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
webpush "github.com/akyoto/webpush-go"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PushSubscription ...
|
|
|
|
type PushSubscription struct {
|
|
|
|
Platform string `json:"platform"`
|
|
|
|
UserAgent string `json:"userAgent"`
|
|
|
|
Screen struct {
|
|
|
|
Width int `json:"width"`
|
|
|
|
Height int `json:"height"`
|
|
|
|
} `json:"screen"`
|
|
|
|
Endpoint string `json:"endpoint" private:"true"`
|
|
|
|
P256DH string `json:"p256dh" private:"true"`
|
|
|
|
Auth string `json:"auth" private:"true"`
|
|
|
|
Created string `json:"created"`
|
|
|
|
LastSuccess string `json:"lastSuccess"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// ID ...
|
|
|
|
func (sub *PushSubscription) ID() string {
|
|
|
|
return sub.Endpoint
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendNotification ...
|
|
|
|
func (sub *PushSubscription) SendNotification(notification *PushNotification) (*http.Response, error) {
|
|
|
|
// Define endpoint and security tokens
|
|
|
|
s := webpush.Subscription{
|
|
|
|
Endpoint: sub.Endpoint,
|
|
|
|
Keys: webpush.Keys{
|
|
|
|
P256dh: sub.P256DH,
|
|
|
|
Auth: sub.Auth,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create notification
|
2019-11-02 16:56:42 +00:00
|
|
|
data, err := json.Marshal(notification)
|
2019-06-03 09:32:43 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Send Notification
|
|
|
|
return webpush.SendNotification(data, &s, &webpush.Options{
|
|
|
|
Subscriber: APIKeys.VAPID.Subject,
|
|
|
|
TTL: 60,
|
|
|
|
VAPIDPrivateKey: APIKeys.VAPID.PrivateKey,
|
|
|
|
})
|
|
|
|
}
|