implement notification click
This commit is contained in:
17
main.go
17
main.go
@@ -63,6 +63,11 @@ type registeredSubscription struct {
|
|||||||
Locations []string `json:"locations"`
|
Locations []string `json:"locations"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type webpushNotificationPayload struct {
|
||||||
|
Summary string `json:"summary"`
|
||||||
|
Location string `json:"location"`
|
||||||
|
}
|
||||||
|
|
||||||
type state struct {
|
type state struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
db *sql.DB
|
db *sql.DB
|
||||||
@@ -621,12 +626,22 @@ func listenForSummaryUpdates(state *state, locKey string) {
|
|||||||
case summary := <-c:
|
case summary := <-c:
|
||||||
log.Printf("sending summary for %v to subscribers...\n", locKey)
|
log.Printf("sending summary for %v to subscribers...\n", locKey)
|
||||||
|
|
||||||
|
payload := webpushNotificationPayload{
|
||||||
|
Summary: summary,
|
||||||
|
Location: locKey,
|
||||||
|
}
|
||||||
|
b, err := json.Marshal(&payload)
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("error creating notification payload: %e\n", err)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
for _, sub := range state.subscriptions[locKey] {
|
for _, sub := range state.subscriptions[locKey] {
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
defer wg.Done()
|
defer wg.Done()
|
||||||
_, err := webpush.SendNotificationWithContext(state.ctx, []byte(summary), sub.Subscription, &opts)
|
_, err := webpush.SendNotificationWithContext(state.ctx, b, sub.Subscription, &opts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("failed to send summary for %v to sub id %v: %e\n", locKey, sub.ID, err)
|
log.Printf("failed to send summary for %v to sub id %v: %e\n", locKey, sub.ID, err)
|
||||||
}
|
}
|
||||||
|
18
web/sw.js
18
web/sw.js
@@ -8,10 +8,26 @@ self.addEventListener('activate', function (event) {
|
|||||||
|
|
||||||
self.addEventListener("push", (event) => {
|
self.addEventListener("push", (event) => {
|
||||||
if (event.data) {
|
if (event.data) {
|
||||||
|
const { summary, location } = event.data.json()
|
||||||
event.waitUntil(
|
event.waitUntil(
|
||||||
self.registration.showNotification("7am weather summary", {
|
self.registration.showNotification("7am weather summary", {
|
||||||
body: event.data.text()
|
data: location,
|
||||||
|
body: summary,
|
||||||
})
|
})
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
self.addEventListener("notificationclick", (event) => {
|
||||||
|
event.notification.close()
|
||||||
|
event.waitUntil(
|
||||||
|
self.clients.matchAll({ type: "window" })
|
||||||
|
.then((clientList) => {
|
||||||
|
const loc = event.notification.data
|
||||||
|
for (const client of clientList) {
|
||||||
|
if (client.url === `/${loc}` && "focus" in client) return client.focus();
|
||||||
|
}
|
||||||
|
if (self.clients.openWindow) return self.clients.openWindow(`/${loc}`);
|
||||||
|
})
|
||||||
|
)
|
||||||
|
})
|
||||||
|
Reference in New Issue
Block a user