Skip to content

Commit

Permalink
Fix browser notifications not showing up on Android (#1974)
Browse files Browse the repository at this point in the history
* Fix browser notifications not showing up on Android

* Use console.error instead of .debug
  • Loading branch information
snowteamer authored May 1, 2024
1 parent 769134d commit 690c7a6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
5 changes: 5 additions & 0 deletions frontend/controller/serviceworkers/sw-primary.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ self.addEventListener('message', function (event) {
}
})

// Handle clicks on notifications issued via registration.showNotification().
self.addEventListener('notificationclick', event => {
console.debug('[sw] Notification clicked:', event.notification)
})

self.addEventListener('push', function (event) {
// PushEvent reference: https://developer.mozilla.org/en-US/docs/Web/API/PushEvent

Expand Down
20 changes: 16 additions & 4 deletions frontend/model/contracts/shared/nativeNotification.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,22 @@ export function makeNotification ({ title, body, icon, path }: {
title: string, body: string, icon?: string, path?: string
}): void {
if (Notification?.permission === 'granted' && sbp('state/vuex/settings').notificationEnabled) {
const notification = new Notification(title, { body, icon })
if (path) {
notification.onclick = function (event) {
sbp('controller/router').push({ path }).catch(console.warn)
try {
const notification = new Notification(title, { body, icon })
if (path) {
notification.onclick = (event) => {
sbp('controller/router').push({ path }).catch(console.warn)
}
}
} catch {
try {
// FIXME: find a cross-browser way to pass the 'path' parameter when the notification is clicked.
navigator.serviceWorker?.ready.then(registration => {
// $FlowFixMe
registration.showNotification(title, { body, icon })
}).catch(console.warn)
} catch (error) {
console.error('makeNotification: ', error.message)
}
}
}
Expand Down

0 comments on commit 690c7a6

Please sign in to comment.