mirror of
https://github.com/binwiederhier/ntfy.git
synced 2024-05-19 11:53:26 +12:00
ff5c854192
- Use new notification request/opt-in flow for push - Implement unsubscribing - Implement muting - Implement emojis in title - Add iOS specific PWA warning - Don’t use websockets when web push is enabled - Fix duplicate notifications - Implement default web push setting - Implement changing subscription type - Implement web push subscription refresh - Implement web push notification click
47 lines
1.1 KiB
JavaScript
47 lines
1.1 KiB
JavaScript
import notifier from "./Notifier";
|
|
import subscriptionManager from "./SubscriptionManager";
|
|
|
|
const onMessage = () => {
|
|
notifier.playSound();
|
|
};
|
|
|
|
const delayMillis = 2000; // 2 seconds
|
|
const intervalMillis = 300000; // 5 minutes
|
|
|
|
class WebPushWorker {
|
|
constructor() {
|
|
this.timer = null;
|
|
}
|
|
|
|
startWorker() {
|
|
if (this.timer !== null) {
|
|
return;
|
|
}
|
|
|
|
this.timer = setInterval(() => this.updateSubscriptions(), intervalMillis);
|
|
setTimeout(() => this.updateSubscriptions(), delayMillis);
|
|
|
|
this.broadcastChannel = new BroadcastChannel("web-push-broadcast");
|
|
this.broadcastChannel.addEventListener("message", onMessage);
|
|
}
|
|
|
|
stopWorker() {
|
|
clearTimeout(this.timer);
|
|
|
|
this.broadcastChannel.removeEventListener("message", onMessage);
|
|
this.broadcastChannel.close();
|
|
}
|
|
|
|
async updateSubscriptions() {
|
|
try {
|
|
console.log("[WebPushBroadcastListener] Refreshing web push subscriptions");
|
|
|
|
await subscriptionManager.refreshWebPushSubscriptions();
|
|
} catch (e) {
|
|
console.error("[WebPushBroadcastListener] Error refreshing web push subscriptions", e);
|
|
}
|
|
}
|
|
}
|
|
|
|
export default new WebPushWorker();
|