1
0
Fork 0
mirror of https://github.com/binwiederhier/ntfy.git synced 2024-12-23 02:02:33 +01:00

Call pushManager.subscribe only if enabled

This commit is contained in:
nimbleghost 2023-06-08 10:55:11 +02:00
parent 46798ac322
commit 2f5acee798
2 changed files with 26 additions and 6 deletions

View file

@ -55,13 +55,26 @@ class Notifier {
const pushManager = await this.pushManager(); const pushManager = await this.pushManager();
return ( const existingSubscription = await pushManager.getSubscription();
(await pushManager.getSubscription()) ??
pushManager.subscribe({ if (existingSubscription) {
return existingSubscription;
}
// create a new subscription only if web push is enabled
// it is possible that web push was previously enabled and then disabled again
// in which case there would be an existingSubscription.
// but if it was _not_ enabled previously, we reach here, and only create a new
// subscription if it is now enabled.
if (await this.pushEnabled()) {
return pushManager.subscribe({
userVisibleOnly: true, userVisibleOnly: true,
applicationServerKey: urlB64ToUint8Array(config.web_push_public_key), applicationServerKey: urlB64ToUint8Array(config.web_push_public_key),
}) });
); }
return undefined;
} }
async pushManager() { async pushManager() {

View file

@ -114,7 +114,14 @@ class SubscriptionManager {
async refreshWebPushSubscriptions(presetTopics) { async refreshWebPushSubscriptions(presetTopics) {
const topics = presetTopics ?? (await this.webPushTopics()); const topics = presetTopics ?? (await this.webPushTopics());
await api.updateWebPushSubscriptions(topics, await notifier.getBrowserSubscription()); const browserSubscription = await notifier.getBrowserSubscription();
if (!browserSubscription) {
console.log("[SubscriptionManager] No browser subscription currently exists, so web push was never enabled. Skipping.");
return;
}
await api.updateWebPushSubscriptions(topics, browserSubscription);
} }
async updateState(subscriptionId, state) { async updateState(subscriptionId, state) {