Skip to content

Commit

Permalink
Add some alerts when push notifications aren't allowed
Browse files Browse the repository at this point in the history
  • Loading branch information
miharekar committed Dec 29, 2024
1 parent 7312c89 commit 77c008a
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions app/javascript/controllers/push_notifications_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ export default class extends Controller {
if (this.#allowed) {
const registration = await this.#serviceWorkerRegistration || await this.#registerServiceWorker()
switch (Notification.permission) {
case "denied": { break }
case "denied": {
alert("Push notifications are blocked. Please update your browser settings to enable notifications.")
break
}
case "granted": { this.#subscribe(registration); break }
case "default": { this.#requestPermissionAndSubscribe(registration) }
}
Expand Down Expand Up @@ -52,9 +55,8 @@ export default class extends Controller {
registration.pushManager
.subscribe({ userVisibleOnly: true, applicationServerKey: this.#vapidPublicKey })
.then(subscription => {

this.#syncPushSubscription(subscription)
this.dispatch("ready")
this.bellTarget.classList.remove("hidden")
})
}

Expand All @@ -68,7 +70,11 @@ export default class extends Controller {

async #requestPermissionAndSubscribe(registration) {
const permission = await Notification.requestPermission()
if (permission === "granted") this.#subscribe(registration)
if (permission === "granted") {
this.#subscribe(registration)
} else {
alert("You need to allow push notifications to enable this feature.")
}
}

get #vapidPublicKey() {
Expand Down

0 comments on commit 77c008a

Please sign in to comment.