| 1 |
import { playSound, topicDisplayName, topicShortUrl, urlB64ToUint8Array } from "./utils"; |
| 2 |
import { notificationTag, toNotificationParams } from "./notificationUtils"; |
| 3 |
import prefs from "./Prefs"; |
| 4 |
import routes from "../components/routes"; |
| 5 |
|
| 6 |
/** |
| 7 |
* The notifier is responsible for displaying desktop notifications. Note that not all modern browsers |
| 8 |
* support this; most importantly, all iOS browsers do not support window.Notification. |
| 9 |
*/ |
| 10 |
class Notifier { |
| 11 |
lastSoundPlayedAt = 0; |
| 12 |
|
| 13 |
async notify(subscription, notification) { |
| 14 |
await this.playSound(); |
| 15 |
|
| 16 |
if (!this.supported()) { |
| 17 |
return; |
| 18 |
} |
| 19 |
|
| 20 |
const shortUrl = topicShortUrl(subscription.baseUrl, subscription.topic); |
| 21 |
const defaultTitle = topicDisplayName(subscription); |
| 22 |
|
| 23 |
console.log(`[Notifier, ${shortUrl}] Displaying notification ${notification.id}`); |
| 24 |
|
| 25 |
const registration = await this.serviceWorkerRegistration(); |
| 26 |
await registration.showNotification( |
| 27 |
...toNotificationParams({ |
| 28 |
message: notification, |
| 29 |
defaultTitle, |
| 30 |
topicRoute: new URL(routes.forSubscription(subscription), window.location.origin).toString(), |
| 31 |
baseUrl: subscription.baseUrl, |
| 32 |
topic: subscription.topic, |
| 33 |
}), |
| 34 |
); |
| 35 |
} |
| 36 |
|
| 37 |
async cancel(subscription, notification) { |
| 38 |
if (!this.supported()) { |
| 39 |
return; |
| 40 |
} |
| 41 |
try { |
| 42 |
const sequenceId = notification.sequence_id || notification.id; |
| 43 |
const tag = notificationTag(subscription.baseUrl, subscription.topic, sequenceId); |
| 44 |
console.log(`[Notifier] Cancelling notification with tag ${tag}`); |
| 45 |
const registration = await this.serviceWorkerRegistration(); |
| 46 |
const notifications = await registration.getNotifications({ tag }); |
| 47 |
notifications.forEach((n) => n.close()); |
| 48 |
} catch (e) { |
| 49 |
console.log(`[Notifier] Error cancelling notification`, e); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
async playSound() { |
| 54 |
// Play sound, but not more than once every 2 seconds |
| 55 |
const now = Date.now(); |
| 56 |
if (now - this.lastSoundPlayedAt < 2000) { |
| 57 |
console.log(`[Notifier] Not playing notification sound, since it was last played <2s ago`, this.lastSoundPlayedAt); |
| 58 |
return; |
| 59 |
} |
| 60 |
const sound = await prefs.sound(); |
| 61 |
if (sound && sound !== "none") { |
| 62 |
try { |
| 63 |
await playSound(sound); |
| 64 |
this.lastSoundPlayedAt = Date.now(); |
| 65 |
} catch (e) { |
| 66 |
console.log(`[Notifier] Error playing audio`, e); |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
async webPushSubscription(hasWebPushTopics) { |
| 72 |
const pushManager = await this.pushManager(); |
| 73 |
const existingSubscription = await pushManager.getSubscription(); |
| 74 |
if (existingSubscription) { |
| 75 |
return existingSubscription; |
| 76 |
} |
| 77 |
|
| 78 |
// Create a new subscription only if there are new topics to subscribe to. It is possible that Web Push |
| 79 |
// was previously enabled and then disabled again in which case there would be an existingSubscription. |
| 80 |
// If, however, it was _not_ enabled previously, we create a new subscription if it is now enabled. |
| 81 |
|
| 82 |
if (hasWebPushTopics) { |
| 83 |
return pushManager.subscribe({ |
| 84 |
userVisibleOnly: true, |
| 85 |
applicationServerKey: urlB64ToUint8Array(config.web_push_public_key), |
| 86 |
}); |
| 87 |
} |
| 88 |
|
| 89 |
return undefined; |
| 90 |
} |
| 91 |
|
| 92 |
async pushManager() { |
| 93 |
return (await this.serviceWorkerRegistration()).pushManager; |
| 94 |
} |
| 95 |
|
| 96 |
async serviceWorkerRegistration() { |
| 97 |
const registration = await navigator.serviceWorker.getRegistration(); |
| 98 |
if (!registration) { |
| 99 |
throw new Error("No service worker registration found"); |
| 100 |
} |
| 101 |
return registration; |
| 102 |
} |
| 103 |
|
| 104 |
notRequested() { |
| 105 |
return this.supported() && Notification.permission === "default"; |
| 106 |
} |
| 107 |
|
| 108 |
granted() { |
| 109 |
return this.supported() && Notification.permission === "granted"; |
| 110 |
} |
| 111 |
|
| 112 |
denied() { |
| 113 |
return this.supported() && Notification.permission === "denied"; |
| 114 |
} |
| 115 |
|
| 116 |
async maybeRequestPermission() { |
| 117 |
if (!this.supported()) { |
| 118 |
return false; |
| 119 |
} |
| 120 |
|
| 121 |
return new Promise((resolve) => { |
| 122 |
Notification.requestPermission((permission) => { |
| 123 |
resolve(permission === "granted"); |
| 124 |
}); |
| 125 |
}); |
| 126 |
} |
| 127 |
|
| 128 |
supported() { |
| 129 |
return this.browserSupported() && this.contextSupported(); |
| 130 |
} |
| 131 |
|
| 132 |
browserSupported() { |
| 133 |
return "Notification" in window; |
| 134 |
} |
| 135 |
|
| 136 |
pushSupported() { |
| 137 |
return config.enable_web_push && "serviceWorker" in navigator && "PushManager" in window; |
| 138 |
} |
| 139 |
|
| 140 |
pushPossible() { |
| 141 |
return this.pushSupported() && this.contextSupported() && this.granted() && !this.iosSupportedButInstallRequired(); |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* Returns true if this is a HTTPS site, or served over localhost. Otherwise the Notification API |
| 146 |
* is not supported, see https://developer.mozilla.org/en-US/docs/Web/API/notification |
| 147 |
*/ |
| 148 |
contextSupported() { |
| 149 |
return window.location.protocol === "https:" || window.location.hostname.match("^127.") || window.location.hostname === "localhost"; |
| 150 |
} |
| 151 |
|
| 152 |
// no PushManager when not installed, but it _is_ supported. |
| 153 |
iosSupportedButInstallRequired() { |
| 154 |
return ( |
| 155 |
config.enable_web_push && |
| 156 |
// a service worker exists |
| 157 |
"serviceWorker" in navigator && |
| 158 |
// but the pushmanager API is missing, which implies we're on an iOS device without installing |
| 159 |
!("PushManager" in window) && |
| 160 |
// check that this is the case by checking for `standalone`, which only exists on Safari |
| 161 |
window.navigator.standalone === false |
| 162 |
); |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
const notifier = new Notifier(); |
| 167 |
export default notifier; |