Skip to content

Commit

Permalink
No icon, cleanup service worker
Browse files Browse the repository at this point in the history
  • Loading branch information
miharekar committed Dec 28, 2024
1 parent ea5d7a0 commit e4a270e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
3 changes: 1 addition & 2 deletions app/jobs/web_push_job.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
class WebPushJob < ApplicationJob
ICON_PATH = "/android-chrome-192x192.png".freeze
CONTACT_EMAIL = "mailto:[email protected]".freeze

queue_as :default

def perform(push_subscription, title:, body:, path: "/")
WebPush.payload_send(
message: {title:, body:, icon: ICON_PATH, data: {path:}}.to_json,
message: {title:, body:, data: {path:}}.to_json,
endpoint: push_subscription.endpoint,
p256dh: push_subscription.p256dh_key,
auth: push_subscription.auth_key,
Expand Down
31 changes: 14 additions & 17 deletions app/views/pwa/service-worker.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,23 @@
self.addEventListener("push", async (event) => {
const data = await event.data.json()
event.waitUntil(
self.registration.showNotification(data.title, {
body: data.body,
icon: data.icon,
data: data.data
})
self.registration.showNotification(data.title, { body: data.body, data: data.data })
)
})

self.addEventListener("notificationclick", (event) => {
event.notification.close()
const url = new URL(event.notification.data?.path || "/", self.location.origin).href
event.waitUntil(openURL(url))
})

const targetPath = event.notification.data?.path || "/"
event.waitUntil(
clients.matchAll({ type: 'window' }).then(windowClients => {
for (let client of windowClients) {
if (client.path === targetPath && 'focus' in client) {
return client.focus()
}
}
return clients.openWindow(targetPath)
})
)
})
async function openURL(url) {
const clients = await self.clients.matchAll({ type: "window" })
const focused = clients.find((client) => client.focused)

if (focused) {
await focused.navigate(url)
} else {
await self.clients.openWindow(url)
}
}

0 comments on commit e4a270e

Please sign in to comment.